为什么不能在data.table中使用`predict`?

Zhaochen He

我试图在data.table中使用predict.lm,并得到一个奇怪的错误。第一部分,数据准备,完美运行。

# (1) Load data
library(data.table)
homeprice = fread('https://vincentarelbundock.github.io/Rdatasets/csv/mosaicData/SaratogaHouses.csv')

# (2) Data Prep: Convert character variables into factors.
myvars = c('heating','fuel','sewer','waterfront','newConstruction','centralAir')
for (var in myvars) {
   homeprice[, paste0(var) := as.factor(get(var))]
}

# (3) Split data into training and test sets
install.packages('caTools')
library(caTools)

homeprice[, split := sample.split(V1, SplitRatio = 0.5)]
train = homeprice[split == T,] # Creating training data
test = homeprice[split == F,] # Create test data


# Train OLS model with training data.
reg1 = lm(price ~ . - V1, train)
summary(reg1) # Displays the results from "myfirstreg"

好的,这是给我带来麻烦的部分:

# In sample-prediction: Predict prices for training set
z = predict(reg1, newdata = train)
train[, price_pred := z] # Works perfectly
train[, price_pred := predict(reg1, newdata = train)] # Gives error

请指教。

Zhaochen He

看来,用于拆分原始数据集的“ split”变量的存在给我们带来了问题。从回归中删除它似乎可以解决问题。

reg1 = lm(price ~ . - V1 - split, train)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我不能在界面中使用默认方法?

为什么我不能在匿名类中使用<Class> .this?

为什么我不能在开关案例中使用枚举?

为什么我不能在Swift中使用let in协议?

为什么我不能在Flutter中使用某些图标?

为什么不能在DELETE语句中使用别名?

为什么notifyAll()不能在线程实例中使用?

为什么不能在分配中使用Unicode字符√和??

为什么复数文字不能在clang中使用?

为什么不能在表达式中使用...语法?

为什么我不能在Spark的mapPartitions中使用foreach

为什么我不能在data.table中使用.I删除当前观察?

为什么不能在写入主机中使用$ _?

为什么我不能在CSS Variable中使用rgba?

为什么不能在let或run中使用continue

为什么我不能在Laravel中使用关系方法?

为什么我不能在列表中使用匿名函数?

为什么不能在递归函数中使用yield

为什么不能在AsyncTask中使用if / else?

为什么我不能在Swift中使用'object == nil'?

为什么限制不能在拆分方法中使用?

为什么不能在PowerShell中使用“查找”?

为什么我不能在TRecord中使用Variable?

为什么通配符不能在Elasticsearch中使用`@`?

为什么不能在QLineEdit对象中使用StyleSheet?

为什么不能在 if 语句中使用空指针?

为什么不能在 for 循环中使用类型提示?

为什么我不能在 Spark 中使用 combineByKey?

为什么不能在函数中使用范围?