I wish to draw 1,000 random samples of size 50 from the dataset and show that E(xi^ui) = 0 holds for each simulated sample. My code is below, and I have been trying to debug for some time now, but I can't figure out what's wrong.
The dataset is called 'dataset' and it has two columns: 'y' and 'x'. I want to regress y on x, get the residuals, and show that they are not correlated with x.
lm.fit <- NA
resid.lm.fit <- NA
correlation <- NA
for (i in 1:1000){
samp1 <- sample(dataset, size=50, replace=T)
lm.fit[i] <- lm(y ~ x, data=samp1)
resid.lm.fit[i]<-resid(lm.fit[i])
correlation[i] <- cor.test(resid.lm.fit[i],samp1$x)
}
The errors I am getting are:
Error in resid.lm.fit[i] <- resid(lm.fit[i]) :
replacement has length zero
In addition: Warning message:
In lm.fit[i] <- lm(y ~ x, data = samp1) :
number of items to replace is not a multiple of replacement length