简体   繁体   English

如何通过在R中使用循环找到RMSE

[英]How to find RMSE by using loop in R

If I have a data frame contain 3 variables :如果我有一个包含 3 个变量的数据框:

origdata <- data.frame(
  age <- c(22, 45, 50, 80, 55, 45, 60, 24,   18, 15),
  bmi <- c(22, 24, 26, 27, 28, 30, 27, 25.5, 18, 25),
  hyp <- c(1,  2,  4,  3,  1,  2,  1,  5,    4,  5) )

I created MCAR (missing complete at random) data :我创建了 MCAR(随机丢失完整)数据:

halpha <- 0.1

# MCAR for attribute (1) age:
mcar <- runif(10, min = 0, max = 1)  
age.mcar <- ifelse(mcar < alpha, NA, origdata$age)  

# MCAR for attribute (2) bmi: 
mcar <- runif(10, min = 0, max = 1) 
bmi.mcar <- ifelse(mcar < alpha, NA, origdata$bmi)  

# MCAR for attribute (3) hyp: 
mcar <- runif(10, min = 0, max = 1) 
hyp.mcar <- ifelse(mcar < alpha, NA, origdata$hyp)  

After that I used the mice package to impute the missing value as follows:从那以后,我用的mice包归咎于缺少的值,如下所示:

install.packages("mice")
library("mice")
imp <- mice(df, 10)              # 10 is mean 10 iteration imputing data 
fill1 <- complete(imp, 1)        # dataset 1
fill2 <- complete(imp, 2)        # dataset 2
allfill <- complete(imp, "long") # all iterations together 

My question is: I want to find RMSE for all 10 datasets individually by using a loop.我的问题是:我想通过使用循环分别为所有 10 个数据集找到 RMSE。 This is my RMSE equation :这是我的 RMSE 方程:

RMSE <- sqrt((sum((origdata - fill)^2)) / sum(is.na(df)))

I mean to make a loop to find the RMSE for each imputed dataset individually:我的意思是做一个循环来分别找到每个估算数据集的 RMSE:
RMSE1 (for dataset #1) RMSE1(对于数据集 #1)
RMSE2 (for dataset #2) RMSE2(对于数据集#2)
... ...
RMSE10 (for dataset #10) RMSE10(对于数据集 #10)

And I also want to know which dataset is best for impute NA s.而且我还想知道哪个数据集最适合插补NA

loop in R: R中的循环:

m <- imp$m  # number of imputations

RSME <- rep(NA, m)
for (i in seq_len(m)) {
  fill <- complete(imp, i)
  RMSE[i] <- (sqrt((sum((orgdata - fill)^2))/sum(is.na(x))))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM