我不明白如何使用predict.lm
命令从线性回归中生成预测值,即使某些依赖变量Y的值缺失,但没有独立的X观察值缺失。代数上,这不是一个问题,但我不知道在R中做到这一点的有效方法。例如,考虑这个虚假数据框和回归模型。我试图在源数据框中分配预测值,但由于一个缺失的Y值而无法这样做:我会得到一个错误。
# Create a fake dataframe
x <- c(1,2,3,4,5,6,7,8,9,10)
y <- c(100,200,300,400,NA,600,700,800,900,100)
df <- as.data.frame(cbind(x,y))
# Regress X and Y
model<-lm(y~x+1)
summary(model)
# Attempt to generate predictions in source dataframe but am unable to.
df$y_ip<-predict.lm(testy)
Error in `$<-.data.frame`(`*tmp*`, y_ip, value = c(221.............
replacement has 9 rows, data has 10
我通过使用代数生成预测结果
df$y<-B0+ B1*df$x
,或 通过调用模型系数生成预测结果 df$y<-((summary(model)$coefficients[1, 1]) + (summary(model)$coefficients[2, 1]*(df$x))
来解决了这个问题;然而,我现在正在处理拥有数百个系数的大数据模型,这些方法已不再实用。我想知道如何使用predict
函数进行预测。
非常感谢您的帮助!
?predict.lm
,您将看到运行函数所需的必要参数以及示例。 - Biranjan