Sorry, didn't cc this to the list.
-------- Forwarded Message --------
Subject: Re: [R] Strange results : bootrstrp CIs
Date: Sat, 13 Jan 2024 17:37:19 -0500
From: Duncan Murdoch <murdoch.duncan at gmail.com>
To: varin sacha <varinsacha at yahoo.fr>
You can debug things like this by setting options(error = recover). That
will drop into the debugger when the error occurs. Examine t.star, r,
and res[[r]] and you will likely see what the problem was.
My guess is that one of the bootstrap samples had a different selection
of countries, so factor(Country) had different levels, and that would
really mess things up.
You'll need to decide how to handle that: If you are trying to estimate
the coefficient for Italy in a sample that contains no data from Italy,
what should the coefficient be? (This is easier for Bayesians to
handle: we don't need data!)
Duncan
On 13/01/2024 5:22 p.m., varin sacha via R-help wrote:> Dear Duncan,
> Dear Ivan,
>
> I really thank you a lot for your response.
> So, if I correctly understand your answers the problem is coming from this
line:
>
> coef(lm(Score~ Time + factor(Country)),data=data[idx,])
>
> This line should be:
> coef(lm(Score~ Time + factor(Country),data=data[idx,]))
>
> If yes, now I get an error message (code here below)! So, it still does not
work.
>
> Error in t.star[r, ] <- res[[r]] :
> ? number of items to replace is not a multiple of replacement length
>
>
> ##########################################
>
Score=c(345,564,467,675,432,346,476,512,567,543,234,435,654,411,356,658,432,345,432,345,
345,456,543,501)
>
> Country=c("Italy", "Italy", "Italy",
"Turkey", "Turkey", "Turkey", "USA",
"USA", "USA", "Korea", "Korea",
"Korea", "Portugal", "Portugal",
"Portugal", "UK", "UK", "UK",
"Poland", "Poland", "Poland", "Austria",
"Austria", "Austria")
>
> Time=c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
>
> e=data.frame(Score, Country, Time)
>
>
> library(boot)
> func= function(data, idx) {
> coef(lm(Score~ Time + factor(Country),data=data[idx,]))
> }
> B= boot(e, func, R=1000)
>
> boot.ci(B, index=2, type="perc")
> #############################################################
>
>
>
>
>
>
>
>
> Le samedi 13 janvier 2024 ? 21:56:58 UTC+1, Ivan Krylov <ikrylov at
disroot.org> a ?crit :
>
>
>
>
>
> ? Sat, 13 Jan 2024 20:33:47 +0000 (UTC)
>
> varin sacha via R-help <r-help at r-project.org> ?????:
>
>> coef(lm(Score~ Time + factor(Country)),data=data[idx,])
>
>
> Wrong place for the data=... argument. You meant to give it to lm(...),
> but in the end it went to coef(...). Without the data=... argument, the
> formula passed to lm() picks up the global variables inherited by the
> func() closure.
>
> Unfortunately, S3 methods really do have to ignore extra arguments they
> don't understand if the class is to be extended, so coef.lm isn't
> allowed to complain to you about it.
>