I have a dataset with variable names like x1,y1,x2,y2 and so on. I would like to regress y1~x1, y2~x2, ..... I want to automatically do this using for loop, so far I have not been successful. Below is the sample code:
x1 <- c(1,2,3,4) y1 <- c(10,20,30,40) x2 <- c(3,6,9,12) y2 <- c(1,2,3,4) dataz <- as.data.frame(cbind(x1,y1,x2,y2)) coef <- rep(NA,2) for (i in 1:2){ coef[i] <- (lm(x[i]~y[i],data=dataz))$coefficients[[2]] } Below is the error I get. Error in eval(expr, envir, enclos) : object 'y' not found.
I tried using paste, but was not sure how to proceed. Any help would be greatly appreciated.