[R] shorter way of coding

Paul Hiemstra paul.hiemstra at knmi.nl
Mon Dec 12 14:42:15 CET 2011


On 12/12/2011 01:16 PM, Mintewab Bezabih wrote:
> Dear R users, 
>
> I am using the code below to generate a fitted value of b. I have about 300 different values for for y (y1, y2, ...y300) which means I will have to write the code below 300 times to generate the 300 different fitted values for y. Is there a short way of doing that ?
>
> Many thanks in advance
> Mintewab 
>
> library(mgcv)
> dat <- read.table("e:/minti's laptop/C/GBG/allround_survey/rainfallGPS.csv", header=T, sep=",") 
> b<-gam(y1~s(x1, x2, k=100),data=dat)
> vis.gam(b)
> fitted(b)
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Hi Mintewab,

Something along these lines should work:

listOfForumlas = paste(1:300, "~s(x1, x2, k=100)")
listofResults = lapply(listOfForumlas, function(f) {
    b<-gam(as.formula(f),data=dat)
    vis.gam(b)
    fitted(b)
  })

But as Sarah already commented, without a reproducible piece of example
code we cannot present any working solutions.

cheers,
Paul

-- 
Paul Hiemstra, Ph.D.
Global Climate Division
Royal Netherlands Meteorological Institute (KNMI)
Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
P.O. Box 201 | 3730 AE | De Bilt
tel: +31 30 2206 494

http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770



More information about the R-help mailing list