0

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.

2

2 Answers 2

2
  • Code below updated to save lm() summaries to variables*

I tried something very simple. I wrote a loop in which I used the "paste0" function to paste "x" and "y" to the iteration number and I used the "get" function to get the objects that the strings referred to. This solution would only work if there is some part of your variable names that's constant and you can iterate through the parts of the variable names that change. By the way, I also changed your data because your x's were perfectly related to your y's, thereby making your covariance matrix blow up, so to speak. Here's the code, I hope it helps:

x1 <- c(runif(40)) y1 <- c(sample(50:300, 40, replace = TRUE)) x2 <- c(runif(40)) y2 <- c(sample(225:975, 40, replace = TRUE)) dataz <- as.data.frame(cbind(x1,y1,x2,y2)) for (i in 1:2){ assign(paste0("coef", i), summary(lm(paste0("x", i, "~", "y", i)))) } 
Sign up to request clarification or add additional context in comments.

4 Comments

Also, I realized you wanted to save your regression summaries to the "coef" variable, so I just replaced the "print" command with the "assign" function to do that in the code. x1 <- c(runif(40)) y1 <- c(sample(50:300, 40, replace = TRUE)) x2 <- c(runif(40)) y2 <- c(sample(225:975, 40, replace = TRUE)) dataz <- as.data.frame(cbind(x1,y1,x2,y2)) for (i in 1:2){ assign(paste0("coef", i), summary(lm(get(paste0("x", i)) ~ get(paste0("y", i))))) }
Fantastic, thank you very much. One question, when I access the summary, instead of variae names I get past0(''x'',i)) instead of x1. Thank you
Sorry about that. That happens because the get() function pulls the dataset that "y1" or "y2" would refer to rather than the name of the variable. Replace your last line in the "for" loop with: assign(paste0("coef", i), summary(lm(paste0("x", i, "~", "y", i)))) I forgot that the lm() function can take a string as an argument.
Check the updated code for the script that will do what you're looking for.
1

Here is a for loop solution which involves explicitly referencing the dataframe column with double brackets. So in fact, the data argument in lm() can be removed:

for (i in 1:2){ coef[i] <- (lm(dataz[[paste0("y", i)]] ~ dataz[[paste0("x", i)]]))$coefficients[[2]] } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.