loop Regression in R, creating new dataframe from excel file.

I have an excel table consisting of 6 independent variables (x) and 12 dependent variables (y) and I have to do linear regression. I have written the code in details for the twelve dependent variables against one of the independent ones, i.e., y1 vs x1, y2 vs x1, ..., y12 vs x1, obtained their plots, coloured then individually and producing the summary statistics.

``````x1 <-read_excel("filename")
plot(x1, y1, type="n", main="y1 vs x1", xlab="x1", ylab="y1", xlim=c(54,59), ylim=c(0,10))
abline(lm(y1~x1))
summary(lm(y1~x1))
summary.aov(lm(y1~x1))

abline(lm(y2~x1))
summary(lm(y2~x1))
summary.aov(lm(y2~x1))
.
.
.
abline(lm(y12~x1))
summary(lm(y12~x1))
summary.aov(lm(y12~x1))
``````

But it is really time consuming so I am working on a loop to do the followings:

1. To carry out all the regression and plot the regression line for each one of the 12 dependent variables against the independent ones, i.e., I have to obtain 6 different graphs each with 12 plots.

I am trying to use the function data.frame to select the columns from my excel file to plots against each other using:

``````library(broom)
library(dplyr)

y1 <- data_frame(x1, x2, x3, x4, x5, x6)

data %>%
group_by(x1) %>%
do(tidy(lm(x2 ~ x3, data = .))) %>%
select(variable = x1, term, t_stat = statistic)
``````
1. Since I have 12 plots on a single graph, col=1,...12, is giving twice many of the colors, I have tried using rainbow but getting lots of errors.
2. I want to store all the summary, R-squared, Adj R-Squared, ... in a new excel table along with the regression equations.

Please can someone help me sort out the codes?

Please do not repost the same question, if you want to attract attention towards your topic, work in providing a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you for the advice. Could this work:
x <- matrix(1:60, 10, 6)
y <- matrix(1:120, 10, 12)

Not really, please read the guide on the link and try to make a proper reproducible example

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.