问题描述
> reg.len <- lm(chao1.ave ~ lg.std.len, b.div) # b.div is my data frame imported from a CSV file
> reg.len
Call:
lm(formula = chao1.ave ~ lg.std.len, data = b.div)
Coefficients:
(Intercept) lg.std.len
282.4 -115.7
> newx <- seq(0.6, 1.4, 0.01)
> prd.len <- predict(reg.len, newdata=data.frame(x=newx), interval="confidence", level=0.90, type="response")
Error in eval(expr, envir, enclos) : object 'lg.std.len' not found
我已经尝试过像这样做lm:lm(b.div$chao1.ave ~ b.div$lg.std.len)
,但是随后,predict()
发出警告,提示newdata和变量的长度不同.因此,我尝试了上述方法,现在predict()
给出了一个错误,指出它无法识别该对象.请如何解决?
I've tried doing the lm like this: lm(b.div$chao1.ave ~ b.div$lg.std.len)
, but then, predict()
gives a warnings that the newdata and variables are different lengths. So, I tried the way above, and now predict()
gives an error saying it doesn't recognize the object. How to fix, please?
推荐答案
Predict期望newdata具有相同的列名(以匹配reg.len中的公式).您要在newdata规范中将其更改为"x",这不是公式的一部分.
Predict expects newdata to have the same column names (to match the formula in reg.len). You're changing it to "x" in your newdata specification, which isn't part of the formula.
dat <- data.frame(y=rnorm(50),lg.std.len=sample(10:15,50,replace=TRUE))
reg.len <- lm(y ~ lg.std.len,data=dat)
newx <- seq(0.6, 1.4, 0.01)
prd.len <- predict(reg.len, newdata=data.frame(lg.std.len=newx),
interval="confidence", level=0.90, type="response")
关键部分是newdata=data.frame(lg.std.len=newx)
这篇关于R:predict.lm()无法识别对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!