For loop for columns in R

I am writing a loop code for go through every column in x to do regression with a specific column in y.
This is my code:

ab <- vector("list", length(x))

for (i in 1:length(x))
  ab[[i]] <- abline(lm(y$Jan, x[[i]])

I have been working on this all day, until I can't fix this part where I keep getting this error:

source('~/.active-rstudio-document', echo=TRUE)
Error in source("~/.active-rstudio-document", echo = TRUE) :
~/.active-rstudio-document:42:0: unexpected end of input
40: ab[[i]] <- abline(lm(y$Jan, x[[i]])
41:
^

How do I fix this error?

I think it would be helpful to know what the structure of the data is. Is x a matrix, a data.frame, a tibble, a list, or what? The best option would be to create a reproducible example: FAQ: What's a reproducible example (`reprex`) and how do I do one? and it would make it a bit easier for us to help

I have extracted x from an excel file, when i type class for x and y, I obtain the results:
[1] "tbl_df" "tbl" "data.frame"
Since I am a beginner in R, I don't know exactly if it is a data.frame exactly.
Right now I have converted both x and y into matrices and trying to make it work.

for (i in 1:ncol(x)){
  ab[[i]] <- abline(lm(y[,1]~x[,i])
                    }

Error: > source('~/.active-rstudio-document', echo=TRUE)
Error in source("~/.active-rstudio-document", echo = TRUE) :
~/.active-rstudio-document:42:21: unexpected '}'
41: ab[[i]] <- abline(lm(y[,1]~x[,i])
42: }
^

Please try to construct a reproducible example as I suggested. First, to use the function lm, you must specify a formula. The function abline adds a line to an existing plot. Is this what you want? Finally, the last line of your code is missing a closing parentheses which is causing the error you are seeing.

I've made some code that works but I'm really not sure it is what you want. What problem are you trying to solve?

library(tidyverse)
nrow <- 6
y <- tibble(Jan=rnorm(nrow))
x <- data.frame(matrix(rnorm(5*nrow), nrow=nrow)) %>% as_tibble()

y
#> # A tibble: 6 x 1
#>        Jan
#>      <dbl>
#> 1 -0.394  
#> 2 -2.27   
#> 3  0.00957
#> 4  1.15   
#> 5  0.642  
#> 6 -0.928
x
#> # A tibble: 6 x 5
#>       X1     X2     X3     X4     X5
#>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
#> 1 -1.88   0.775  0.249  2.05   1.70 
#> 2  0.512  1.14   0.233 -1.32  -0.513
#> 3 -0.620  0.462 -1.24   0.436  0.485
#> 4  0.491  1.22  -0.113  0.213 -0.166
#> 5 -1.00  -0.913  0.252  0.101  0.546
#> 6  0.201 -0.221 -0.533  0.948 -0.318

ab <- vector("list", length(x))

for (i in 1:length(x)){
  dat <- data.frame(y=y$Jan, x=x[[i]])
  ab[[i]] <- lm(y~x, data=dat)
                    }
                    
ab
#> [[1]]
#> 
#> Call:
#> lm(formula = y ~ x, data = dat)
#> 
#> Coefficients:
#> (Intercept)            x  
#>     -0.4128      -0.3009  
#> 
#> 
#> [[2]]
#> 
#> Call:
#> lm(formula = y ~ x, data = dat)
#> 
#> Coefficients:
#> (Intercept)            x  
#>     -0.1642      -0.3263  
#> 
#> 
#> [[3]]
#> 
#> Call:
#> lm(formula = y ~ x, data = dat)
#> 
#> Coefficients:
#> (Intercept)            x  
#>     -0.3467      -0.2533  
#> 
#> 
#> [[4]]
#> 
#> Call:
#> lm(formula = y ~ x, data = dat)
#> 
#> Coefficients:
#> (Intercept)            x  
#>      -0.453        0.383  
#> 
#> 
#> [[5]]
#> 
#> Call:
#> lm(formula = y ~ x, data = dat)
#> 
#> Coefficients:
#> (Intercept)            x  
#>     -0.4260       0.4434

Created on 2020-03-30 by the reprex package (v0.3.0)

I am doing linear regression. I have 6 independent variables and 12 dependent ones. I have to plot the regression line of each dependent variables y against each dependent variable x.
I want to plot all the 12 y against a specific x on a single graph, and keep repeating for every other x. So in total i should have six graphs, one for each x.
And I want to store the statistics results in an excel file.

x <- matrix(1:60, 10, 6)
y <- matrix(1:120, 10, 12)
plot(
  x,
  y,
  type = "n",
  main = "y vs x",
  xlab = "x",
  ylab = "y"
)

for (i in 1:ncol(x)) {
  ab[[i]] <- abline(lm(y[, 1] ~ x[, i])
}

Count your open and close brackets. Brackets must balance.

1 Like

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