使用 R 的 Caret 包计算预测值的预测区间

Calculate Prediction Intervals of a predicted value using Caret package of R

我在 Caret 包中使用了不同的神经网络包来进行预测。 nnet 包使用的代码是

    library(caret)
    # training model using nnet method
    data <- na.omit(data)
    xtrain <- data[,c("temperature","prevday1","prevday2","prev_instant1","prev_instant2","prev_2_hour")]
    ytrain <- data$power
    train_model <- train(x = xtrain, y = ytrain, method = "nnet", linout=TRUE, na.action = na.exclude,trace=FALSE)
    # prediction using training model created
    pred_ob <- predict(train_model, newdata=dframe,type="raw")

预测函数只是计算预测值。但是,我也需要预测区间 (2-sigma)。在搜索时,我在 link 找到了相关答案,但这并不是我所需要的结果。该解决方案建议使用 finalModel 变量作为

predict(train_model$finalModel, newdata=dframe, interval = "confidence",type=raw)

还有其他计算预测区间的方法吗?使用的训练数据是我上一个问题 dput() 和我的预测数据帧(测试数据)的 dput()

    dframe <- structure(list(temperature = 27, prevday1 = 1607.69296666667, 
    prevday2 = 1766.18103333333, prev_instant1 = 1717.19306666667, 
    prev_instant2 = 1577.168915, prev_2_hour = 1370.14983583333), .Names = c("temperature", 
"prevday1", "prevday2", "prev_instant1", "prev_instant2", "prev_2_hour"
), class = "data.frame", row.names = c(NA, -1L))

******************************更新****************** *******

我按照 link 中的建议使用了 nnetpredint 软件包。令我惊讶的是,它会导致一个错误,我发现这个错误很难调试。这是我到目前为止更新的代码,

library(nnetpredint)
nnetPredInt(train_model, xTrain = xtrain, yTrain = ytrain,newData = dframe)

它导致以下错误:

Error: Number of observations for xTrain, yTrain, yFit are not the same
[1] 0

我可以检查 xtrainytraindframe 的尺寸是否正确,但我不知道 yFit。根据 nnetpredintvignette

的例子,我不需要这个

您可以使用 nnetPredInt 函数 {package:nnetpredint}。查看函数的帮助页面 here

caret 不生成预测区间;依赖于单独的包。如果那个包不能做到这一点,那么 train 对象也不能。我同意 nnetPredInt 是合适的方法。

另外两个注意事项:

  • 如果您还没有的话,您很可能应该将数据居中和缩放。
  • 使用 finalModel 对象有些危险,因为它不知道在创建数据之前对数据做了什么(例如虚拟变量、居中和缩放或其他预处理方法等)。

最大

感谢您的提问。对您的问题的简单回答是:目前 nnetPredInt 函数仅支持以下 S3 对象,"nnet"、"nn" 和 "rsnns",由不同的神经网络包产生。插入符包 return 中的 train 函数是一个 "train" 对象。这就是为什么函数 nnetPredInt 没有从您的 train_model.

中获取 yFit 向量,即训练数据集的 fitted.value

1.Quick 使用 caret 包中的模型的方法: 从 'train' 对象中获取最终模型结果:

    nnetObj = train_model$finalModel # return the 'nnet' model which the caret package has found.
    yPredInt = nnetPredInt(nnetObj, xTrain = xtrain, yTrain = ytrain,newData = dframe)

例如,使用 Iris 数据集和 caret 包中的 'nnet' 方法进行回归预测。

    library(caret)
    library(nnetpredint)
    # Setosa 0 and Versicolor 1
    ird <- data.frame(rbind(iris3[,,1], iris3[,,2]), species = c(rep(0, 50), rep(1, 50)))
    samp = sample(1:100, 80)
    xtrain = ird[samp,][1:4]
    ytrain = ird[samp,]$species
    # Training
    train_model <- train(x = xtrain, y = ytrain, method = "nnet", linout = FALSE, na.action = na.exclude,trace=FALSE)
    class(train_model) # [1] "train"

    nnetObj = train_model$finalModel
    class(nnetObj) # [1] "nnet.formula" "nnet" 

    # Constructing Prediction Interval
    xtest = ird[-samp,][1:4]
    ytest = ird[-samp,]$species
    yPredInt = nnetPredInt(nnetObj, xTrain = xtrain, yTrain = ytrain,newData = xtest)

    # Compare Results: ytest and yPredInt
    ytest
    yPredInt

2.The 辛苦了

使用通用的 nnetPredInt 函数将所有神经网络特定参数传递给函数:

    nnetPredInt(object = NULL, xTrain, yTrain, yFit, node, wts, newData,alpha = 0.05 , lambda = 0.5, funName = 'sigmoid', ...)

    xTrain # Training Dataset
    yTrain # Training Target Value
    yFit # Fitted Value of the training data

    node # Structure of your network, like c(4,5,5,1)
    wts # Specific order of weights parameters found by your neural network
    newData # New Data for prediction

小贴士: 现在 nnetpredint 包只支持带有激活输出的标准多层神经网络回归,不支持线性输出, 后续会支持更多机型

如果您愿意编写自己的实现,还有另一种选择。您可以使用与为标准非线性回归编写的相同实现从经过训练的网络中获取预测区间(假设使用反向传播进行估计)。

本文介绍了方法论并且相当直接:http://www.cis.upenn.edu/~ungar/Datamining/Publications/yale.pdf

与所有事情一样,这种方法也有一些缺点(在论文中概述),但作为一种选择绝对值得了解。