I need to repeat a process 1000 times and save the results as I go along, but I’m not sure how to do it.
Here’s what I have:
x1 = runif(4000, min = 0, max = 1)
x2 = runif(4000, min = 0, max = 1)
y <- 1*x1 - 2*x2 + rnorm(4000)
df <- data.frame(y, x1, x2)
# part that’s needs to be replicated 1k times:
set.seed(2)
df2 <- df[sample(1 : nrow(df), 4000, replace = T ), ]
x= as.matrix(df2[,-1])
y= as.matrix(df2[,1])
OLS <- solve(t(x)%*%x)%*%t(x)%*%y
# What I think might work
set.seed(2)
n = 1000
out <- replicate(n, {df2 <- df[sample(1 : nrow(df), 4000, replace = T ), ]
})
The problem is that I cannot figure out how to code the x and y matrix, and the OLS estimate inside the replicate()
function. Perhaps a loop would be better?
What is the most efficient way to do this?