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)
This isn't a reproducible example, so I can't provide code, but I would put all the y variables in a list and use lapply(). Sarah On Mon, Dec 12, 2011 at 8:16 AM, Mintewab Bezabih <Mintewab.Bezabih at economics.gu.se> 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)-- Sarah Goslee http://www.functionaldiversity.org
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
Hi> > 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 ?With lm you can use several dependent variables to get result, but I do not know if it works with gam. You can put y1 - y300 to list and than use lapply or for cycle to do the analysis and store results in a list (list.y). something like (untested) for (i in 1:300) { b[i] <- gam(list.y[i]~s(x1,x2, k=100, data=dat) } Regards Petr> > 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.