ames_housing_data$NbrPred <- pred[[1]]
会解决的。
关于您的错误:
prd
是一个变量名(如您所定义的!)。将数据帧传递给
mean
,这会导致错误。
如果我是你,我会写下面的代码:
NbrMLR <- lm(SalePrice ~ Neighborhood, data = ames_housing_data)
ames_housing_data$NbrPred <- predict(NbrMLR, ames_housing_data)
ames_housing_data$NbrRes <- SalePrice - ames_housing_data$NbrPred
ames_housing_data$absoluteNbrRes <- abs(ames_housing_data$NbrRes)
NbrMAE <- mean(ames_housing_data$absoluteNbrRes)