0

I am trying to compile a code that runs a model on each column and saves each prediction into a data.frame.

I am probably missing a basic step because the result only saves data for the last prediction. It would be great if someone could give me a tip.

Here is the code:

for(i in 1:ncol(temp[,1:101])){          
  pred=(exp(predict(gam(temp[,i]~s(w),gamma=1.4,data=temp))))
  prediction[i]<- as.data.frame(cbind(pred=pred))
}

Here is a short look of the data and result:

temp2
      tdat.V1    tdat.V2     tdat.V3     tdat.V4     tdat.V5  w
9   0.2468596  0.2468596 -0.47226384 -0.47226384 -0.69767176  9
10 -0.3298719 -0.3298719 -0.61766160 -0.61766160 -1.05190065 10
11  0.2636122  0.2636122 -0.16966523 -0.16966523 -0.98531224 11
12  1.1036205  1.1036205  0.46601526  0.46601526 -0.35346974 12
13  1.1337664  1.1337664 -0.31946816 -0.31946816 -0.78722896 13
14  1.0441290  1.0441290 -0.19397040 -0.19397040 -0.99997758 14
15  0.5904416  0.5904416 -0.49903362 -0.49903362 -1.29327665 15
16  0.2704478  0.2704478 -0.33188601 -0.33188601 -0.89020267 16
17  0.4905354  0.4905354  0.26849660  0.26849660 -0.22608949 17
18  1.4072215  1.4072215  1.43372101  1.43372101  0.74552152 18
19 -0.3510362 -0.3510362 -0.65455175 -0.65455175 -0.67979925 19
20 -0.9471780 -0.9471780 -0.99449245 -0.99449245 -0.94800264 20
21  0.5601007  0.5601007 -0.41078889 -0.41078889 -0.70911666 21
22  0.6337811  0.6337811  0.11769665  0.11769665 -0.37718872 22
23  1.1154420  1.1154420  0.52692499  0.52692499  0.26777430 23
24  0.1314404  0.1314404  0.02146546  0.02146546 -0.03748099 24
25  0.2262661  0.2262661  0.14216196  0.14216196 -0.19273456 25
26  1.7767008  1.7767008  1.19683315  1.19683315  0.55529405 26
27  2.0070761  2.0070761  1.70737151  1.70737151  0.90322033 27
28  2.2252446  2.2252446  1.35160191  1.35160191  0.98155994 28
29  1.7452878  1.7452878  0.86052298  0.86052298  1.27898872 29
30  0.2071554  0.2071554  0.55612163  0.55612163  0.64726184 30
31  1.7144228  1.7144228  0.74949354  0.74949354  0.42433658 31
32  0.2533343  0.2533343 -0.11861726 -0.11861726 -0.63511376 32
33  0.6176735  0.6176735  0.29274750  0.29274750 -0.20402280 33
34  1.0868382  1.0868382  1.19325652  1.19325652  1.57309478 34
35  1.7051584  1.7051584  0.00151082  0.00151082 -0.95416617 35
>  for(i in 1:ncol(temp2[,1:4])){ 
+    
+    pred=(exp(predict(gam(temp2[,i]~s(w),gamma=1.4,data=temp2))))
+    prediction <- as.data.frame(cbind(pred=pred))
+ }
> print(prediction)
        pred
9  0.6969407
10 0.7291379
11 0.7628225
12 0.7980632
13 0.8349320
14 0.8735040
15 0.9138580
16 0.9560762
17 1.0002448
18 1.0464540
19 1.0947979
20 1.1453751
21 1.1982889
22 1.2536473
23 1.3115630
24 1.3721544
25 1.4355449
26 1.5018639
27 1.5712468
28 1.6438349
29 1.7197765
30 1.7992264
31 1.8823468
32 1.9693071
33 2.0602847
34 2.1554654
35 2.2550432
tsg
  • 21
  • 4
  • Where is `prediction` first created? –  Feb 18 '15 at 10:46
  • In "pred" for the first column. – tsg Feb 18 '15 at 10:54
  • Can you provide code on a minimal working example of the temp, and other parts (do you introduce "prediction" beforehand)? `dput()` is useful for this. – puslet88 Feb 18 '15 at 10:59
  • if `prediction` is `data.frame` then I guess you should add a comma `prediction[,i]` – ClementWalter Feb 18 '15 at 11:06
  • @puslet88 please see the working example. – tsg Feb 18 '15 at 11:37
  • 1
    @clemlaflemme thanks it seems that prediction[,i] did the trick. – tsg Feb 18 '15 at 11:40
  • @tsg, thanks. Try to make it so that it can be evaluated by any user. Instead of using the print command, you can print out the variable by `dput()` and then you can add that structure onto a variable for example, or make an R object of the same type. It would be best, if anyone can get all the required variables just by copying the code from your post and evaluate them. In this case they should get the same error. – puslet88 Feb 18 '15 at 13:42

0 Answers0