1

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?

bandcar
  • 649
  • 4
  • 11

1 Answers1

0

Add a simplify=F to replicate, which will save the data frames in a list. You can add multiple commands into one call.

df2=replicate(n,{
  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
  df[sample(1 : nrow(df), 4000, replace = T ), ]
},simplify=F)
user2974951
  • 9,535
  • 1
  • 17
  • 24
  • I also need to do this part 1000 times. `x= as.matrix(df2[,-1]) y= as.matrix(df2[,1]) OLS <- solve(t(x)%*%x)%*%t(x)%*%y` how can I add it in? – bandcar Sep 06 '22 at 12:38
  • I’ll adjust my post so it’s more clear that the matrix portion should be part of the replication! – bandcar Sep 06 '22 at 13:19
  • Would it be `df2=replicate(n, {df[sample(1 : nrow(df), 4000, replace = T ), ]},simplify=F, OLS <- solve(t(x)%*%x)%*%t(x)%*%y )`? I don’t have my laptop with me right now, but I’d love to hear your thoughts! – bandcar Sep 06 '22 at 15:17