简体   繁体   中英

The RMSE returns either NA or error as a vector or list

I am calculating the RMSE for imputed values however it returns NA or the following error:

Not compatible with requested type: [type=list; target=double].

Here is the dataset:

#X 
[1] 7.833134 5.983825 4.933213 3.928198 3.546371 3.678339 4.981000 5.991927 5.302597 6.057735 6.471139 7.080655
[13] 8.411150 5.800234 5.732390 4.497880 3.609478 4.059384 5.046262 5.268591 5.575791 5.772434 7.139264 7.612555
[25]       NA       NA       NA 1.378538       NA       NA

#Z
 [1] 7.833134 5.983825 4.933213 3.928198 3.546371 3.678339 4.981000 5.991927 5.302597 6.057735 6.471139 7.080655
[13] 8.411150 5.800234 5.732390 4.497880 3.609478 4.059384 5.046262 5.268591 5.575791 5.772434 7.139264 7.612555
[25] 4.933213 5.732390 4.981000 1.378538 3.609478 3.678339

then using RMSE:

rmse(x, z) #NA

#or
#when you create these as a dataframe
rmse(x[, , drop=FALSE], z[, , drop=FALSE])

>Not compatible with requested type: [type=list; target=double].

I am supposing this is the result of having NAs, so would converting the NAs to zero then set the 'right' calculation? Or are there approximation errors that I should look out for regarding this?

I'm not sure if rmse is from a particular package, or if you wrote it yourself, but it is likely that the internal call to mean does not include the argument na.rm = TRUE , which should discard those comparisons. The following modified function, rmse2 , includes this as the default argument:

# function
rmse2 <- function(x, y, na.rm = TRUE){
  res <- sqrt(mean((x-y)^2, na.rm = na.rm))
  return(res)
}

# generate data
n <- 10
set.seed(1)
x <- rnorm(n)
x[7:9] <- NaN # add NAs
z <- rnorm(n)

# visualize data
cbind(x, z)
#                x           z
#  [1,] -0.6264538  1.51178117
#  [2,]  0.1836433  0.38984324
#  [3,] -0.8356286 -0.62124058
#  [4,]  1.5952808 -2.21469989
#  [5,]  0.3295078  1.12493092
#  [6,] -0.8204684 -0.04493361
#  [7,]        NaN -0.01619026
#  [8,]        NaN  0.94383621
#  [9,]        NaN  0.82122120
# [10,] -0.3053884  0.59390132

# call to function               
rmse2(x = x, y = z)
# [1] 1.741073

rmse2(x = x, y = z, na.rm = FALSE)
# [1] NaN

But... seeing that you are doing this between the original vector and the imputed one does not really make sense. The non-NA values are exactly the same, so rmse will be zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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