简体   繁体   English

计算模拟线性回归的 RMSE

[英]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:代码中有几个问题:

  • Assuming that you are trying to learn/predict 'y' based on 'M', you have to combine M and y and make a data frame.假设您正在尝试根据 'M' 学习/预测 'y',您必须将 M 和 y 结合起来制作一个数据框。
  • After that only, you should split first 150 for train and remaining for test.仅在此之后,您应该将前 150 个拆分为训练,其余为测试。
  • Then you train on train.data and predict on test.data然后你在train.datatrain.data并在test.data上预测
  • Also, since you have hardcoded [1:150] and [150:200] for train-test split, you will have to pass 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.

相关问题 计算二元线性回归的系数 - Calculating coefficients of bivariate linear regression 计算多元线性回归的预测 - Calculating predictions for multiple linear regression 在循环中迭代计算线性回归 - Calculating linear regression with iteration in a loop 在模拟线性回归中,为什么标准偏差与变量的平方根成正比? - In a simulated linear regression why is the standard deviation proportional to the square root of the variable? 通过平均值栅格计算线性回归和斜率 - Calculating linear regression and slope through rasterstack of averages 计算双变量线性回归时出错 - Error when calculating for bivariate linear regression 在 R 中计算 RMSE 时遇到问题 - Trouble calculating RMSE in R 在R中,可以使用MAE(平均绝对误差)代替RMSE作为线性回归的成本函数(lm / glm) - In R is it possible to use MAE (Mean Absolute Error) instead of RMSE as the cost function to a linear regression (lm/glm) 同一横坐标值下,如何计算坐标点与非线性回归之间的均方根误差和标准差? - How to calculate RMSE and standard deviation between a coordinate point and a non-linear regression under the same abscissa value? 正交线性回归(总最小二乘)拟合,在R中得到RMSE和R平方 - Orthogonal Linear Regression (total least squares) fit, get RMSE and R-squared in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM