[R] Strange error message, many thanks

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sat Mar 21 17:48:23 CET 2020


On 21/03/2020 12:35 p.m., varin sacha via R-help wrote:
> # # # # # # # # # # # # # # # # # # # # # # # #
> install.packages( "robustbase",dependencies=TRUE )
> install.packages( "boot",dependencies=TRUE )
> library(boot)
> library(robustbase)
> 
> n<-500
> b<-runif(n, 0, 5)
> z <- rnorm(n, 2, 3)
> a <- runif(n, 0, 5)
> df<-data.frame(b,z,a)
> 
> y_model<- 0.1*b - 0.5 * z - a + 10
> y_obs <- y_model +c( rnorm(n*0.9, 0, 0.1), rnorm(n*0.1, 0, 0.5) )
> 
>   # function to obtain MSE
>   MSE <- function(data, indices, formula) {
>      d <- data[indices, ] # allows boot to select sample
>      fit <- lmrob(formula, data = d)
>      ypred <- predict(fit)
>      mean((d[["y_obs "]] - ypred)^2)
>   }
> 
>   # Make the results reproducible
>   set.seed(1234)
>   
>   # bootstrapping with 500 replications
>   results <- boot(data = df, statistic = MSE,
>                    R = 500, formula = y_obs ~ b+z+a)
> 
> boot.ci(results, type="bca")
> # # # # # # # # # # # # # # # # # # # # # # # # #

Try using debug(MSE), and you'll see that d[["yobs "]] doesn't exist, so 
your MSE function always returns NaN.

Duncan Murdoch



More information about the R-help mailing list