[英]Calculating RMSE for Simulated Linear Regression
I am trying to calculate the RMSE for the simulated data.我正在尝试计算模拟数据的 RMSE。 But the output gives NaN for the RMSE.但是输出给出了 RMSE 的 NaN。 Below is the code I am using.下面是我正在使用的代码。
library(caret)
RMSE <- function(x,y) sqrt(mean((x-y)^2))
sim.regression<-function(n.obs=200,coefficients=c(3,1.5,0,0,2,0,0,0),s.deviation=.1){
n.var=length(coefficients)
M=matrix(0,ncol=n.var,nrow=n.obs)
beta=as.matrix(coefficients)
for (i in 1:n.var){
M[,i]=rnorm(n.obs,0,1)
}
y=M %*% beta + rnorm(n.obs,0,s.deviation)
train.data<-y[1:150]
train.data<-data.frame(train.data)
test.data<-y[151:200]
test.data<-data.frame(test.data)
prediction <- predict(lm(y~M),test.data)
RMSE.data<-RMSE(prediction, test.data$y)
return (list(x=M,y=y,coeff=coefficients, RMSE=RMSE.data))
}
set.seed(2000)
sim.regression(100)
Welcome to SO.欢迎来到 SO。 There were few issues in the code:代码中有几个问题:
train.data
and predict on test.data
然后你在train.data
上train.data
并在test.data
上预测200
as in sim.regression(200).此外,由于您已硬编码 [1:150] 和 [150:200] 用于训练测试拆分,您将必须像 sim.regression(200) 一样通过200
。Corrected code below:更正以下代码:
library(caret)
RMSE <- function(x,y) sqrt(mean((x-y)^2))
sim.regression<-function(n.obs=200,coefficients=c(3,1.5,0,0,2,0,0,0),s.deviation=.1){
n.var=length(coefficients)
M=matrix(0,ncol=n.var,nrow=n.obs)
beta=as.matrix(coefficients)
for (i in 1:n.var){
M[,i]=rnorm(n.obs,0,1)
}
y=M %*% beta + rnorm(n.obs,0,s.deviation)
data<-data.frame(M,y)
train.data <- data[1:150,]
test.data<-data[151:200,]
prediction <- predict(lm(y~., data=train.data),test.data)
RMSE.data<-RMSE(prediction, test.data$y)
return (list(x=M,y=y,coeff=coefficients, RMSE=RMSE.data))
}
set.seed(2000)
sim.regression(200)
Prints:印刷:
$RMSE
0.0755869850491716
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.