Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat the re-sampling function for 1000 times ? Using lapply?

Please me out! I appreciate any helps ! Thanks!

I have trouble on repeat doing re-sampling for 1000 times. I tried using replicate() to do that but it's not working. Is there any other method to do that? Can anyone show me if this maybe done by using lapply? Following is my code:

#sampling 1000 betas0 & 1 (coefficients) from the data
get.beta=function(data,indices){ 
  data=data[indices,] #let boot to select sample
  lm.out=lm(y ~ x,data=data)
  return(lm.out$coefficients)
}
n=nrow(data)
get.beta(data,1:n)

bootcoe=boot(data,get.beta,R=1000) #generate 1000 random samples
head(bootcoe$t) #look at the betas

From the above code I can get 1000 betas0 & 1 by random sampling the data. And I would like to do that 1000 times to get different betas. How should I do that besides replicate()?

like image 277
user2978129 Avatar asked Dec 10 '13 09:12

user2978129


People also ask

How do you repeat a sample in R?

How do you repeat something in R? We can use the repeat function, in R, according to the following template: repeat { if(condition) { break } } Code language: R (r) sum < 0 repeat{ sum < sum + 1 print(sum) if (sum == 10){ break } }

What does replicate function do in R?

replicate() function in R Programming Language is used to evaluate an expression N number of times repeatedly.


1 Answers

This is more of an extended comment where I demonstrate that replicate should work. Here's an example of a CLT. Just replace your lines what's between the curly braces.

x <- replicate(1000, {
  mm <- runif(10)
  mean(mm)
  })
hist(x)

enter image description here

like image 163
Roman Luštrik Avatar answered Oct 09 '22 09:10

Roman Luštrik