[R] Monte Carlo on simple regression

Eric Berger er|cjberger @end|ng |rom gm@||@com
Wed Aug 22 18:06:18 CEST 2018


Li is defined as d1$a which is a vector. You should use

N <- length(Li)

HTH,
Eric


On Wed, Aug 22, 2018 at 6:02 PM, Ogbos Okike <giftedlife2014 using gmail.com>
wrote:

> Kind R-users,
> I run a simple regression. I am interested in using the Monte Carlo to test
> the slope parameter.
> Here is what I have done:
> d1<-read.table("Lightcor",col.names=c("a"))
> d2<-read.table("CRcor",col.names=c("a"))
>  Li<-d1$a
> CR<-d2$a
>
>  fit<-lm(Li~CR)
>  a<-summary(fit)
> a gives the slope as 88.15
>
> Problem: I now what to repeat the samples to access this coefficient.
> Following one of the related examples I got online, I did (tried to
> modify):
>
> N <- nrow(Li) # returns the number of observations in the dataset
> C <- 50         # desired number of subsamples
> S <- 38         # desired sample size
>
> sumb2 <- 0
> for (i in 1:C){   # a loop over the number of subsamples
>   set.seed(3*i)   # a different seed for each subsample
>   subsample <- Li[sample(1:N, size=S, replace=TRUE), ]
>   mod <- lm(Li~CR,data=subsample)
>   #sum b2 for all subsamples:
>   sumb2 <- sumb2 + coef(mod)[[2]]
> }
> print(sumb2/C, digits = 3)
>
>    But when I run the script, I had error message:
> Error in 1:N : argument of length 0
> My data:
> Li        CR
> 74281 8449
> 92473 8148
> 62310 8520
> 71219 8264
> 33469 8389
> 75768 7499
> 61636 7821
> 103829 8468
> 87336 8568
> 129443 8190
> 97682 8539
> 106918 8502
> 97171 8578
> 48012 8181
> 93086 8631
> 92374 8562
> 113010 8404
> 66956 8592
> 133037 8632
> 108849 8644
> 81544 8442
> 105072 8615
> 143437 7724
> 153294 7829
> 123735 8682
> 154738 8756
> 100760 8839
> 108034 8839
> 81826 8858
> 116901 8847
> 80780 8869
> 122684 8736
> 141716 9087
> 144315 9166
> 162078 9147
> 163184 9267
> 150688 9275
> 200848 9259
> 221570 8943
> 192424 8564
> 173024 9282
> 197326 9318
> 209344 9293
> 220201 9242
> 212626 9324
> 218115 9319
> 170001 9314
> 187490 9346
> 172440 9350
> 180330 9349
> 200807 9355
> 234994 9350
> 139053 9284
> 150048 9361
> 203650 9346
> 233331 9369
> 198790 9340
> 164060 9382
> 198000 9401
> 201707 9355
> 179257 9369
> 188736 9298
> 243392 9393
> 246040 9374
> 269058 9364
> 201657 9370
> 187942 9354
> 228514 9305
> 234000 9392
> 224431 9395
> 163502 9398
> I would be most glad for your great assistance.
> Many thanks.
> Ogbos
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org 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.
>

	[[alternative HTML version deleted]]




More information about the R-help mailing list