Home > OS >  Compute error with two datasets using For loop
Compute error with two datasets using For loop

Time:02-14

I have 2 datasets that have the same column names.

One dataframe is predicted that has 12 forecasted values for each variable. The other dataframe is real that has the 12 real values for each variable.

I am trying to make this for loop to print the Mean Squared Forecasted Error.

for (i in names(predicted)){
  print(paste("The Mean Squared Forecast Error for",i,"is:",mean((real$i-predicted$i)^2)))
}

This is the desired output is:

[1] "The Mean Squared Forecast Error for EMPL is: 2.22973256477949e-06"
[1] "The Mean Squared Forecast Error for CPI is: 5.15514699434535e-06"
[1] "The Mean Squared Forecast Error for FFR is: 0.0295111061402315"

I don't know why the for loop gives me that the calculation is NaN

Thanks in advance!


Data

> dput(real)
structure(list(CPI = c(0.00160433859088815, -0.00215862261991973, 
-4.59020776588659e-06, -0.000538046208090392, 0.00265772082268079, 
-0.00054730999077357, -0.00265434619923699, -0.000529520203581768, 
0.00263754860468435, 0.00208864328030867, -0.000548981800946535, 
-0.00470589103741226), EMPL = c(0.000440619330092318, 0.000505389630054864, 
-0.00064261089956652, 0.00163820296223705, 0.00123949686537728, 
0.00231633116306718, 0.00274100555496126, 0.000121807479846581, 
-0.000616354925266904, 0.00175489535365614, 0.00356476054636445, 
-0.000756181462504557), FFR = c(0.02, 0.01, -0.01, 0, 0, 0.03, 
0.23, 0.17, 0.18, 0.15, 0.17, 0.23)), class = "data.frame", row.names = c(NA, 
-12L))
> dput(predicted)
structure(list(CPI = c(-0.00185325936075232, -0.000205612911651649, 
0.000515554281526663, 0.000832059799115526, -9.59291577299353e-05, 
-0.000135397534643682, -0.000319969700328926, -0.000531443546314851, 
0.000334709759798288, 0.000149004361515839, -0.000190722371242093, 
-6.3736415706231e-05), EMPL = c(0.00180490246150916, 0.00155636832886101, 
0.00142059674176869, 0.00104389866040043, 0.0016108887176987, 
0.00152450457655764, 0.00106805136354765, 0.00151749850981702, 
0.00126307148334478, 0.00121395424088581, 0.0013782559420265, 
0.00149406940983166), FFR = c(0.0664586752084257, 0.228921245733048, 
0.135151339424908, -0.162398412959252, -0.128277890243452, -0.134292810077186, 
-0.049475881436482, 0.0442366320282881, -0.0376918344723898, 
-0.018180368465285, 0.0655870731024621, 0.0492813528000579)), class = "data.frame", row.names = c(NA, 
-12L))

CodePudding user response:

Please note that if i <- colname, then data$i does not produce the corresponding column. Use data[[i]] instead.

for (i in names(predicted)){
  print(paste("The Mean Squared Forecast Error for",i,"is:",mean((real[[i]]-predicted[[i]])^2)))
}
  • Related