簡體   English   中英

使用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)。 在搜索時,我在stackoverflow鏈接中找到了一個相關的答案,但這並沒有根據需要得到。 解決方案建議使用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沒有任何了解。 根據nnetpredint小插圖的示例,我不需要這個

caret不會生成預測間隔; 依賴於單個包裝。 如果那個包裹不能做到這一點,那么train也不能反對。 我同意nnetPredInt是合適的方法。

另外兩個注意事項:

  • 如果您尚未對數據進行居中和擴展,則很可能應該這樣做。
  • 使用finalModel對象有些危險,因為它不知道在創建數據之前對數據做了什么(例如,虛擬變量,居中和縮放或其他預處理方法等)。

最高

感謝您的提問。 對您的問題的一個簡單答案是:現在, nnetPredInt函數僅支持由不同的神經網絡軟件包生成的以下S3對象“ nnet”,“ nn”和“ rsnns” 插入符包中的train函數返回一個“ train”對象。 這就是為什么函數nnetPredInt無法從您的train_model中獲取yFit向量(即訓練數據集的fit.value)的原因。

1.從插入符號包中使用模型的快速方法:從“火車”對象獲取finalModel結果:

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

例如,使用Iris數據集和插入符號包中的“ 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,艱難的道路

使用通用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軟件包僅支持具有激活輸出的標准多層神經網絡回歸,而不支持線性輸出,並且它將在不久的將來支持更多類型的模型。

您可以使用nnetPredInt函數{package:nnetpredint}。 在此處查看功能的幫助頁面

如果您願意編寫自己的實現,則還有另一種選擇。 您可以使用為標准非線性回歸編寫的相同實現方式,從經過訓練的網絡中獲得預測間隔(假設使用反向傳播進行估算)。

本文介紹了該方法,並且很直接: http//www.cis.upenn.edu/~ungar/Datamining/Publications/yale.pdf

與所有方法一樣,此方法也有一些缺點(本文概述),但是絕對值得一試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM