我試圖預測,使用R代碼的時間序列殘差的殘差。我的數據集有以下兩列(我會放一個樣品與第10行):支持向量機 - R的代碼 - 預測時間序列
Observation Residuals
1 -0,087527458
2 -0,06907199
3 -0,066604145
4 -0,07796713
5 -0,081723932
6 -0,094046868
7 -0,101535816
8 -0,101884203
9 -0,11131246
10 -0,092548176
對於我建立一個支持向量機使用R中的預測:
# Load the data from the csv file
dataDirectory <- "C://"
data <- read.csv(paste(dataDirectory, "Data_SVM_Test.csv", sep=""),sep=";", header = TRUE)
head(data)
# Plot the data
plot(data, pch=16)
# Create a linear regression model
model <- lm(Residuals ~ Observation, data)
# Add the fitted line
abline(model)
predictedY <- predict(model, data)
# display the predictions
points(data$Observation, predictedY, col = "blue", pch=4)
# This function will compute the RMSE
rmse <- function(error)
{
sqrt(mean(error^2))
}
error <- model$residuals # same as data$Y - predictedY
predictionRMSE <- rmse(error) # 5.70377
plot(data, pch=16)
plot.new()
# svr model ==============================================
if(require(e1071)){
model <- svm(Residuals ~ Observation , data)
predictedY <- predict(model, data)
points(data$Observation, predictedY, col = "red", pch=4)
# /!\ this time svrModel$residuals is not the same as data$Y - predictedY
# so we compute the error like this
error <- data$Residuals - predictedY
svrPredictionRMSE <- rmse(error) # 3.157061
}
當我執行上面的代碼我收到以下錯誤消息,並沒有任何輸出:
Warning message:
In Ops.factor(data$Residuals, predictedY) : ‘-’ not meaningful for factors
任何人都有一個想法如何解決這個錯誤?
非常感謝!
關於你的答案非常感謝。所以你建議將殘差列轉換爲數字,對吧?在問題的開頭我把我的數據集:) –
我建議使用'誤差< - 數據$殘差 - as.numeric(predictedY)''因爲將predictedY'類是因素當你與SVM預測。 – LyzandeR