0

I want the coefficients looped across all the columns of data frame. I want to save this output into a variable which I can place into a data frame, where all the values can be placed into one column.

Printed output shows many NA's. How can this be removed in the process.

So something like:

Co_data <- (Looped values)

Example of code:

mod1 <- lm(starling_farm ~ years, data = L_farmland_frame)

for(i in colnames(L_population.frame)){

print(c(mod1$coefficients[i], 
mod2$coefficients[i], 
mod3$coefficients[i], 
mod4$coefficients[i], 
mod5$coefficients[i], 
mod6$coefficients[i], 
mod7$coefficients[i], 
mod8$coefficients[i], 
mod9$coefficients[i], 
mod10$coefficients[i], 
mod11$coefficients[i], 
mod12$coefficients[i], 
mod13$coefficients[i], 
mod14$coefficients[i], 
mod15$coefficients[i], 
mod16$coefficients[i],
mod17$coefficients[i]))} ```


Output value:

```years       years       years       years       years 
-0.17263280 -0.16783959  0.04566027 -0.11492727 -0.11345848 
      years       years       years       years       years 
-0.15713866 -0.11703737 -0.08202416 -0.25307321 -0.07794717 
      years       years       years       years       years 
-0.07591691 -0.12998199 -0.10227772 -0.03500562 -0.10366356 
      years       years 
-0.13475428 -0.14861171 
<NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> 
  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA 
<NA> <NA> <NA> <NA> <NA> 
  NA   NA   NA   NA   NA 
<NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> 
  NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA   NA 
....
....
sm925
  • 2,648
  • 1
  • 16
  • 28
Lime
  • 738
  • 5
  • 17

1 Answers1

1

I don't have an idea of the structure of your dataset (please edit your question to add a reproducible example of your data.

However, if you want to extract the coefficients of lm function accross all columns, you can do:

Data:

df <- mtcars[c(1:10),]

The dataset looks like:

> df
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

Then, you can apply the lm function on df accross all columns by doing : (here, we will calculate lm by comparing each columns to the column mpg)

mod <- apply(df,2,function(x){lm(x~mpg, data = df)$coefficients[1]})

and you get:

> mod
          mpg           cyl          disp            hp          drat 
 4.493867e-15  1.470741e+01  6.987361e+02  4.449931e+02  1.866482e+00 
           wt          qsec            vs            am          gear 
 4.834655e+00  1.035858e+01 -1.135418e+00 -6.882239e-01  1.195054e+00 
         carb 
 6.021719e+00 

Finally, you can do a dataframe using:

final_df = data.frame(conditions = colnames(df),Coefficient = mod)

Does it answer your question ?

EDIT - removing NA

If you have some NA values, you can remove it by doing:

mod <- mod[!is.na(mod)]
dc37
  • 15,840
  • 4
  • 15
  • 32