title: Having an issue in saving fitted and observed values in lm model?
body: <p>I run a linear regression model with looping over the categorical levels of one column of my data. I want to save some of the model summary output.</p>
  <p>I achieved to save some of them include coefficients, R2 and Pvalue. However, when I save the fitted and observed values in a data.frame called <code>obsFit</code>, it returns the fitted and observed values of only one categorical level as I need <code>7</code> levels.</p>
  <p>Any ideas and thoughts?</p>
  <p>So sorry for hardcoding R, I am still a novice who is in the learning process.</p>
  <p>Here is the chuck of <code>code</code> I used.</p>
  <pre><code># Wilayah 1 vs MADA4 model
  &gt; Y1 &lt;- yield[ ,2:5]
  &gt; mada4 &lt;- stns[[&quot;S1&quot;]][[&quot;Mada4_sum&quot;]]
  &gt; unique(mada4$aggre_per)
  [1] &quot;3&quot;     &quot;4&quot;     &quot;5&quot;     &quot;6&quot;     &quot;7&quot;     &quot;mamjj&quot;
  [7] &quot;amj&quot;  
  &gt; 
  &gt; # create an empty matrix to fill results
  &gt; df &lt;- matrix(rep(NA, 9))
  &gt; 
  &gt; for (agg.per in unique(mada4$aggre_per)) {
  +   
  +   subdata &lt;- subset(mada4, aggre_per == agg.per)
  +   subdata &lt;- cbind(subdata, Y1$I_S1)
  +   names(subdata)[4] &lt;- &quot;I_S1&quot;
  +   
  +   #save model summary
  +   mod &lt;- lm('I_S1 ~ value', subdata)
  +   slope     &lt;- summary(mod)[[4]][2,1]
  +   intercept &lt;- summary(mod)[[4]][1,1]
  +   r2        &lt;- summary(mod)[[8]]
  +   adjr2     &lt;- summary(mod)[[9]]
  +   fstat     &lt;- summary(mod)[[10]][[1]]
  +   pval      &lt;- summary(mod)[[4]][2,4]
  +   pearson   &lt;- cor(subdata$I_S1,subdata$value )
  +   res &lt;- c(&quot;Mada4_sum&quot;, agg.per, slope, intercept, r2, adjr2, fstat, pval, pearson)
  +   df &lt;- cbind(df, res)
  +   
  +   #save the fitted and observed yields as data.frame
  +   obsFit &lt;- data.frame(cbind(&quot;Mada4_sum&quot;, agg.per,subdata$I_S1, fitted(mod)))
  +   
  + }
  &gt; 
  &gt; df &lt;- data.frame(t(df[,-1]))
  &gt; df[,-2:-1] &lt;- apply (df[,-2:-1], 2, as.numeric)
  &gt; df[,-2:-1] &lt;- apply (df[,-2:-1], 2, round, digit =2)
  &gt; rownames(df) &lt;- 1:nrow(df)
  &gt; names(df) &lt;-c( &quot;WI&quot;,
  +                &quot;aggre_per&quot;,
  +                &quot;slope&quot;,
  +                &quot;intercept&quot;,
  +                &quot;R2&quot;,
  +                &quot;adjustedR2&quot;,
  +                &quot;F-stat&quot;,
  +                &quot;p-value&quot;,
  +                &quot;correlation&quot;)
  &gt; head(df)
           WI aggre_per slope intercept   R2 adjustedR2
  1 Mada4_sum         3 -1.17   6230.91 0.09       0.03
  2 Mada4_sum         4 -0.27   6128.81 0.00      -0.07
  3 Mada4_sum         5 -1.04   6240.46 0.02      -0.05
  4 Mada4_sum         6 -2.88   6507.82 0.14       0.08
  5 Mada4_sum         7 -1.12   6296.72 0.05      -0.02
  6 Mada4_sum     mamjj -0.89   6760.92 0.18       0.12
    F-stat p-value correlation
  1   1.40    0.26       -0.30
  2   0.07    0.80       -0.07
  3   0.23    0.64       -0.13
  4   2.26    0.15       -0.37
  5   0.76    0.40       -0.23
  6   3.12    0.10       -0.43
  &gt; #check levels of `aggre_per`
  &gt; unique(df$aggre_per)
  [1] &quot;3&quot;     &quot;4&quot;     &quot;5&quot;     &quot;6&quot;     &quot;7&quot;     &quot;mamjj&quot;
  [7] &quot;amj&quot;  
  &gt; 
  &gt; names(obsFit) &lt;- c(&quot;WI&quot;, &quot;Aggr.per&quot;, &quot;Observed&quot;, &quot;Fitted&quot;)
  &gt; print(obsFit)
             WI Aggr.per Observed           Fitted
  103 Mada4_sum      amj     5204 6019.05130014577
  104 Mada4_sum      amj     5824 6061.66361454587
  105 Mada4_sum      amj     5481 6129.49546195825
  106 Mada4_sum      amj     5587 5962.52476063545
  107 Mada4_sum      amj     6260 6124.27762754192
  108 Mada4_sum      amj     5771   6017.312022007
  109 Mada4_sum      amj     5899 5888.60543973734
  110 Mada4_sum      amj     6338 6189.50055774613
  111 Mada4_sum      amj     6340 6023.39949549272
  112 Mada4_sum      amj     6013 6146.01860427665
  113 Mada4_sum      amj     6075 6221.67720331355
  114 Mada4_sum      amj     6484 5959.91584342728
  115 Mada4_sum      amj     6771 5998.17996248043
  116 Mada4_sum      amj     6493 6249.50565353401
  117 Mada4_sum      amj     6386 6290.37868979533
  118 Mada4_sum      amj     6465 6109.49376336229
  &gt; 
  </code></pre>
  <p>Hereis the <code>dput</code> of my data?</p>
  <pre><code>&gt; dput(mada4)
  structure(list(year = c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 
  2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2001, 2002, 
  2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 
  2014, 2015, 2016, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
  2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2001, 2002, 2003, 
  2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
  2015, 2016, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
  2010, 2011, 2012, 2013, 2014, 2015, 2016, 2001, 2002, 2003, 2004, 
  2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 
  2016, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
  2011, 2012, 2013, 2014, 2015, 2016), aggre_per = c(&quot;3&quot;, &quot;3&quot;, 
  &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, 
  &quot;3&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, 
  &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;4&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, 
  &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;5&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, 
  &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;6&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, 
  &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, &quot;7&quot;, 
  &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, 
  &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;mamjj&quot;, 
  &quot;mamjj&quot;, &quot;mamjj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, 
  &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;, &quot;amj&quot;
  ), value = c(189, 6, 212, 81, 101, 84, 163, 198, 143, 128, 456, 
  93, 37, 23, 58, 0, 210, 196, 70, 122, 132, 184, 387, 57, 232, 
  153, 11, 325, 228, 75, 41, 31, 141, 95, 219, 217, 178, 154, 139, 
  112, 164, 62, 106, 202, 101, 123, 108, 252, 175, 186, 110, 252, 
  95, 190, 150, 161, 125, 165, 176, 67, 221, 63, 65, 139, 100, 
  267, 207, 202, 48, 322, 240, 250, 270, 298, 159, 164, 178, 29, 
  94, 164, 815, 750, 818, 874, 554, 934, 1079, 778, 934, 806, 908, 
  851, 765, 313, 366, 586, 526, 477, 399, 591, 405, 528, 676, 330, 
  521, 380, 293, 594, 550, 261, 214, 422)), row.names = c(1L, 2L, 
  3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
  18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
  31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 
  45L, 46L, 47L, 48L, 49L, 50L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 
  59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 69L, 70L, 71L, 72L, 
  73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 86L, 
  87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 
  100L, 101L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 
  112L, 113L, 114L, 115L, 116L, 117L, 118L), class = &quot;data.frame&quot;)
  &gt; 
  </code></pre>
  <pre><code>&gt; dput(yield)
  structure(list(date = structure(c(11323, 11688, 12053, 12418, 
  12784, 13149, 13514, 13879, 14245, 14610, 14975, 15340, 15706, 
  16071, 16436, 16801), class = &quot;Date&quot;), I_S1 = c(5204, 5824, 5481, 
  5587, 6260, 5771, 5899, 6338, 6340, 6013, 6075, 6484, 6771, 6493, 
  6386, 6465), II_S1 = c(5081, 5674, 5589, 5809, 6055, 6231, 5728, 
  5971, 6268, 5999, 6015, 6624, 6827, 6453, 6382, 6340), III_S1 = c(5071, 
  5349, 5703, 5451, 5764, 5528, 5629, 5395, 5797, 5785, 5887, 5581, 
  5899, 6179, 6086, 5734), IV_S1 = c(5515, 5491, 5969, 5971, 6265, 
  6242, 6153, 6188, 6311, 6447, 6213, 6410, 6744, 6754, 6247, 6415
  ), I_S2 = c(4735, 5227, 5415, 5894, 3294, 5113, 6056, 6216, 5752, 
  5758, 5540, 5418, 6461, 5346, 6437, 5633), II_S2 = c(4837, 5209, 
  5111, 5482, 3203, 5389, 5209, 6041, 5617, 5927, 4993, 5342, 6380, 
  5122, 6320, 5470), III_S2 = c(4720, 4986, 4846, 4720, 3968, 4912, 
  4437, 5675, 5368, 5638, 4892, 4849, 6117, 4996, 5586, 4666), 
      IV_S2 = c(5054, 5584, 5546, 5666, 5116, 6288, 6089, 6529, 
      5880, 6156, 5263, 5358, 6804, 5212, 6174, 5560)), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -16L))
  </code></pre>