我试图预测,使用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