Strange error message, many thanks

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Strange error message, many thanks

R help mailing list-2
Dear R-experts,

Here below my R code giving an error message that I don't understand. If somebody can help me to fix it, it would be highly appreciated.

# # # # # # # # # # # # # # # # # # # # # # # #
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")
# # # # # # # # # # # # # # # # # # # # # # # # #

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange error message, many thanks

Duncan Murdoch-2
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange error message, many thanks

David Winsemius
In reply to this post by R help mailing list-2

On 3/21/20 9:35 AM, varin sacha via R-help wrote:

> Dear R-experts,
>
> Here below my R code giving an error message that I don't understand. If somebody can help me to fix it, it would be highly appreciated.
>
> # # # # # # # # # # # # # # # # # # # # # # # #
> 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) )
Two errors: First, you need to have the y_obs vector in the df dataframe
if you later reference it as a column.
>
>   # 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)
The second error is the misspelling . Remove the space

>   }
>
>   # 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")


Now get:


 > boot.ci(results, type="bca")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 500 bootstrap replicates

CALL :
boot.ci(boot.out = results, type = "bca")

Intervals :
Level       BCa
95%   ( 0.0237,  0.0460 )
Calculations and Intervals on Original Scale
Some BCa intervals may be unstable

--

David.

> # # # # # # # # # # # # # # # # # # # # # # # # #
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange error message, many thanks

R help mailing list-2
Hi Duncan, Hi David,

Many thanks, ok for the first error but David when you say "The second error is the misspelling . Remove the space". Where do I have to remove the space ?

Best,
Sacha


Le samedi 21 mars 2020 à 18:14:30 UTC+1, David Winsemius <[hidden email]> a écrit :






On 3/21/20 9:35 AM, varin sacha via R-help wrote:

> Dear R-experts,
>
> Here below my R code giving an error message that I don't understand. If somebody can help me to fix it, it would be highly appreciated.
>
> # # # # # # # # # # # # # # # # # # # # # # # #
> 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) )
Two errors: First, you need to have the y_obs vector in the df dataframe
if you later reference it as a column.
>
>   # 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)
The second error is the misspelling . Remove the space

>   }
>
>   # 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")


Now get:



> boot.ci(results, type="bca")

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 500 bootstrap replicates

CALL :
boot.ci(boot.out = results, type = "bca")

Intervals :
Level       BCa
95%   ( 0.0237,  0.0460 )
Calculations and Intervals on Original Scale
Some BCa intervals may be unstable

--

David.

> # # # # # # # # # # # # # # # # # # # # # # # # #
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange error message, many thanks

David Winsemius

On 3/21/20 12:18 PM, varin sacha wrote:
>   mean((d[["y_obs "]] - ypred)^2)

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange error message, many thanks

R help mailing list-2
Hi David,

Perfect I got it now. One last precision, if I want to get the MSE value how can I get it ?







Le samedi 21 mars 2020 à 20:26:37 UTC+1, David Winsemius <[hidden email]> a écrit :









On 3/21/20 12:18 PM, varin sacha wrote:


>   mean((d[["y_obs "]] - ypred)^2)

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange error message, many thanks

David Winsemius

On 3/21/20 12:56 PM, varin sacha wrote:
> Hi David,
>
> Perfect I got it now. One last precision, if I want to get the MSE value how can I get it ?


If I understand your question, it is answered in the help page:

help(boot,pack=boot)

# See the "Value" section

  str(results)

# List of 11
# $ t0       : num 0.0335
# $ t        : num [1:500, 1] 0.0424 0.0334 0.0289 0.0224 0.0369 ...

Snipped rest of output.

--

David

>
>
>
>
>
>
>
> Le samedi 21 mars 2020 à 20:26:37 UTC+1, David Winsemius <[hidden email]> a écrit :
>
>
>
>
>
>
>
>
>
> On 3/21/20 12:18 PM, varin sacha wrote:
>
>
>>     mean((d[["y_obs "]] - ypred)^2)

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Strange error message, many thanks

R help mailing list-2
David,

Great... exactly what I was looking for... Many thanks.




Le samedi 21 mars 2020 à 21:12:31 UTC+1, David Winsemius <[hidden email]> a écrit :






On 3/21/20 12:56 PM, varin sacha wrote:
> Hi David,
>
> Perfect I got it now. One last precision, if I want to get the MSE value how can I get it ?


If I understand your question, it is answered in the help page:

help(boot,pack=boot)

# See the "Value" section

 str(results)

# List of 11
# $ t0       : num 0.0335
# $ t        : num [1:500, 1] 0.0424 0.0334 0.0289 0.0224 0.0369 ...

Snipped rest of output.


--

David

>
>
>
>
>
>
>
> Le samedi 21 mars 2020 à 20:26:37 UTC+1, David Winsemius <[hidden email]> a écrit :
>
>
>
>
>
>
>
>
>
> On 3/21/20 12:18 PM, varin sacha wrote:
>
>
>>     mean((d[["y_obs "]] - ypred)^2)

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.