Question

I created a pair of models from my dataset (800 rows, 2 colums):

#1
m1=lm(A~A1, fish)
#2
mmars1=polymars(fish$A, fish)
#3
bestm1=loess.smooth(fish$A1, fish$A, span=best)
#4
bestm2=smooth.spline(fish$A1,fish$A, spar=best)

and then i tried to predict an y using a new x:

#Predict
#1
predict(m1, data.frame(xl=c(100000)))
#2
predict(mmars1, data.frame(xm=c(100000)))
#3
predict(bestm1, data.frame(xb1=c(100000)))
#4
predict(bestm2, data.frame(xb2=c(100000)))

#4 works fine, but i have problems with the other 3. #1 and #2 return 800 values instead of 1. #3 gives me this error.

Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "list"

What am i doing wrong with #1 and #2? Is there an alternative way to use the predict() method with loess.smooth?

Was it helpful?

Solution

When using predict your variables must have the same name they had when calling lm. In #1, you use lm with variable name A1 but then use predict with variable name xl. The predict function doesn't understand your new data. Imagine what would happen with several independent variables...you need to stick to their names.

Use predict(m1, list(A1=100000)) instead.

As to #2, I've never used package polspline before, but it seems that your call to polymars is wrong. The second argument must be the predictors, and as of now you include the response as a predictor because you give the full data.frame. So you should do:

mmars1 <- polymars(fish$A, fish$A1)
predict(mmars1, fish$A1)

Same for #3. It seems that loess.smooth already gives you the fitted points. You can use the evaluation parameter to tell how many points along your predictor you want to fit:

bestm1 <- loess.smooth(fish$A1, fish$A, evaluation=50)

will give you a list of 50 points along A1 with the fitted points in A. Alternatively, you can use loess instead of loess.smooth:

bestm1 <- loess(A~A1, fish)
predict(bestm1, data.frame(A1=100000))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top