[英]I don't understand R's predict
I created a pair of models from my dataset (800 rows, 2 colums): 我从数据集中创建了一对模型(800行,2列):
#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: 然后我尝试使用新的x来预测y:
#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. #4正常工作,但其他3却有问题。#1和#2返回800个值,而不是1。#3给了我这个错误。
Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "list"
UseMethod(“ predict”)中的错误:没有适用于“预测”的适用方法应用于“列表”类的对象
What am i doing wrong with #1 and #2? #1和#2我在做什么错? Is there an alternative way to use the
predict()
method with loess.smooth
? 是否有另一种方法可以将
loess.smooth
与predict()
方法一起loess.smooth
?
When using predict
your variables must have the same name they had when calling lm
. 使用
predict
变量必须具有与调用lm
时相同的名称。 In #1, you use lm
with variable name A1
but then use predict
with variable name xl
. 在#1中,您将
lm
与变量名A1
使用,然后将predict
与变量名xl
。 The predict
function doesn't understand your new data. predict
功能无法理解您的新数据。 Imagine what would happen with several independent variables...you need to stick to their names. 想象一下几个独立变量会发生什么……您需要坚持使用它们的名称。
Use predict(m1, list(A1=100000))
instead. 请改用
predict(m1, list(A1=100000))
。
As to #2, I've never used package polspline
before, but it seems that your call to polymars
is wrong. 关于#2,我以前从未使用过
polspline
软件包,但是看来您对polymars
的调用是错误的。 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. 第二个参数必须是预测变量,到目前为止,因为您提供了完整的data.frame,所以您将响应包含在预测变量中。 So you should do:
因此,您应该执行以下操作:
mmars1 <- polymars(fish$A, fish$A1)
predict(mmars1, fish$A1)
Same for #3. 与#3相同。 It seems that
loess.smooth
already gives you the fitted points. 看来
loess.smooth
已经为您提供合适的点。 You can use the evaluation
parameter to tell how many points along your predictor you want to fit: 您可以使用
evaluation
参数来告诉您要拟合预测变量的多少点:
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
. 会给你沿着50点列表
A1
与拟合点A
。 Alternatively, you can use loess
instead of loess.smooth
: 另外,您可以使用
loess
代替loess.smooth
:
bestm1 <- loess(A~A1, fish)
predict(bestm1, data.frame(A1=100000))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.