Dear R-experts, Here below, my R code working BUT I get a strange result I was not expecting! Indeed, the?95% percentile bootstrap CIs is (-54.81, -54.81 ). Is anything going wrong? Best, ########################################## 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") #############################################################
On 13/01/2024 3:33 p.m., varin sacha via R-help wrote:> 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")Your function ignores the data, because it passes data[idx,] to coef(), not to lm(). coef() ignores it. So the function is using the global variables you created earlier, not the ones in e. Duncan Murdoch
? 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. -- Best regards, Ivan