我已经看到这个问题出现了很多,但还没有找到令人满意的解决方案,特别是对于我的情况。
我正在使用 pls() 包在 R 中运行偏最小二乘回归,然后想使用拟合模型在 newdata 上使用 RMSEP() 计算预测的均方根误差。这会引发错误,我相信这特别是因为我正在对函数进行如下编码:
plsr( Y ~ X [随便什么,随便什么] ...
我需要索引dataframe $ X的特定部分。这是一个例子:
library(pls)
gasoline <- gasoline
#Split dataframe between training and testing data
set.seed(123)
split <- sample.split(gasoline$octane, SplitRatio = 0.70)
gasoline$train <- split
gas.fit <- plsr(octane ~ NIR[ ,1:10] + NIR[ ,20:30],
ncomp = 10,
data = gasoline[gasoline$train ,],
validation = "LOO",
scale = FALSE,
center = TRUE,
method = "simpls"
)
#I can use RMSEP() on the fitted model
RMSEP(gas.fit)
#I can use the fitted model to predict octane of my test set
predict(gas.fit, newdata = gasoline[!gasoline$train ,])
#But I cannot get the RMSEP of the test predictions
RMSEP(gas.fit, estimate = "test", newdata = gasoline[!gasoline$train ,])
最后一个命令引发错误:
eval 中的错误(predvars、data、env):找不到对象“NIR”
我所知道 的:我知道应该存在对象“NIR”,因为我选择将训练和测试数据组合到一个数据帧中。
RMSEP() 函数在“plsr( Y ~ X[whatever , whatever ]”样式的模型上运行良好,只要您不调用 newdata。predict() 函数在这两种情况下都运行良好。
我试过的: Mevik & Wehrens (2007) 坚持我们使用格式
plsr( octane ~ NIR,
...
data = gasoline
...)
并不是
plsr( gasoline$octane ~ gasoline$NIR,
这更类似于我在示例中所做的,但不完全相同。即便如此,我还是尝试了以下调整:
gas.fit <- plsr(octane ~ NIR,
ncomp = 10,
data = c(
gasoline[gasoline$train ,]$NIR[ , 1:10],gasoline[gasoline$train ,]$NIR[ ,20:30]
),
validation = "LOO",
scale = FALSE,
center = TRUE,
method = "simpls"
)
但这也不好(“环境”不是长度为一);这也意味着我还必须包含额外的汽油$辛烷,这进一步违反了长度标准。
我真的很想找到这种方法的解决方案,因为我的最终使用目标是将 plsr() 模型包含在样式的 for() 循环中:
gas.fit <- plsr(octane ~ NIR[ ,i:(i+20)],
作为移动窗口 PLSR 算法的一部分。