Skip to content

shorter way of coding

10 messages · Paul Hiemstra, PIKAL Petr, Mintewab Bezabih +1 more

#
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:

  
    
#
On 12/12/2011 01:16 PM, Mintewab Bezabih wrote:
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
#
Hi
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
http://www.R-project.org/posting-guide.html
#
Dear Paul and Sarah

Thanks for the suggestion. I have provided my data here in to make the results reproducable. I am actually trying to do interpoliation of climate data where x1 and x2 are my latitude and longitude and sum64-sum 368 are my rainfall observations which I need to regress against x1 and x2. In the previous I was trying to get my story clear so I did not go into details. 

Now when I run your suggestion below, I get the following error message. Is there anything in your instruction that I did not get right?
many thanks
mintewab

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)
  })

Error in model.frame.default(formula = 1 ~ 1 + x1+ x2, data = dat,  : 
  variable lengths differ (found for 'x1')
________________________________________
Fr?n: Paul Hiemstra [paul.hiemstra at knmi.nl]
Skickat: den 12 december 2011 14:42
Till: Mintewab Bezabih
Kopia: r-help at r-project.org
?mne: Re: [R] shorter way of coding
On 12/12/2011 01:16 PM, Mintewab Bezabih wrote:
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
#
Dear Paul and Sarah

Thanks for the suggestion. I have provided my data here in to make the results reproducable. I am actually trying to do interpoliation of climate data where x1 and x2 are my latitude and longitude and sum64-sum 368 are my rainfall observations which I need to regress against x1 and x2. In the previous I was trying to get my story clear so I did not go into details. 

Now when I run your suggestion below, I get the following error message. Is there anything in your instruction that I did not get right?
many thanks
mintewab

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)
  })

Error in model.frame.default(formula = 1 ~ 1 + x1+ x2, data = dat,  : 
  variable lengths differ (found for 'x1')

________________________________________
Fr?n: Paul Hiemstra [paul.hiemstra at knmi.nl]
Skickat: den 12 december 2011 14:42
Till: Mintewab Bezabih
Kopia: r-help at r-project.org
?mne: Re: [R] shorter way of coding
On 12/12/2011 01:16 PM, Mintewab Bezabih wrote:
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
#
That's not a reproducible example. Paul suggested a list of formulas,
but I recommended creating a list of y variables.

In your attempt, you didn't include the y in the name of the dependent
variable; that's probably why it doesn't work.

Look at this:
$y1

Call:
lm(formula = y ~ x1)

Coefficients:
(Intercept)           x1
    0.56392     -0.02586


[[2]]

Call:
lm(formula = y ~ x1)

Coefficients:
(Intercept)           x1
    0.66375     -0.03519


[[3]]

Call:
lm(formula = y ~ x1)

Coefficients:
(Intercept)           x1
    0.29106      0.02845

Sarah

On Mon, Dec 12, 2011 at 11:21 AM, Mintewab Bezabih
<Mintewab.Bezabih at economics.gu.se> wrote:
#
Dear Sarah and R users, 

Thanks Sarah for the suggestion. The example you gave me works and I managed to get it to run by modifying the 'lm' to the gam command. 

y.list <- list(y1=runif(10), y2 <- runif(10), y3 <- runif(10))
x1 <- 1:10
x2 <- c(11, 15, 17, 2, 18, 6, 7, 8, 12, 10)
lapply(y.list, function(y)gam(y~s(x1,x2, k=100)))


I now face problem of getting the fitted results from the gam command. My original problem for only one y looked like this. Now how do I get to incorporate vis.gam(b) and fitted (b) into my new code? 

b<-gam(y~s(x1,x2,k=100),data=dat)
vis.gam(b)
fitted(b)

Many thanks once again.
Mintewab
________________________________________
Fr?n: Sarah Goslee [sarah.goslee at gmail.com]
Skickat: den 12 december 2011 17:36
Till: Mintewab Bezabih
Kopia: r-help at r-project.org
?mne: Re: [R] shorter way of coding

That's not a reproducible example. Paul suggested a list of formulas,
but I recommended creating a list of y variables.

In your attempt, you didn't include the y in the name of the dependent
variable; that's probably why it doesn't work.

Look at this:
$y1

Call:
lm(formula = y ~ x1)

Coefficients:
(Intercept)           x1
    0.56392     -0.02586


[[2]]

Call:
lm(formula = y ~ x1)

Coefficients:
(Intercept)           x1
    0.66375     -0.03519


[[3]]

Call:
lm(formula = y ~ x1)

Coefficients:
(Intercept)           x1
    0.29106      0.02845

Sarah

On Mon, Dec 12, 2011 at 11:21 AM, Mintewab Bezabih
<Mintewab.Bezabih at economics.gu.se> wrote:
--
Sarah Goslee
http://www.functionaldiversity.org
#
In exactly the same way:

y.gam <- lapply(y.list, function(y)gam(y~s(x1,x2, k=1))) # list of gam objects
y.vis.gam <- lapply(y.gam, vis.gam) # list of vis.gam outputs
y.fitted <- lapply(y.gam, fitted) # list of fitted values.

Saraj

On Tue, Dec 13, 2011 at 10:48 AM, Mintewab Bezabih
<Mintewab.Bezabih at economics.gu.se> wrote:

  
    
#
Thanks so much Sarah. Everything worked now!

Mintewab
________________________________________
Fr?n: Sarah Goslee [sarah.goslee at gmail.com]
Skickat: den 13 december 2011 17:36
Till: Mintewab Bezabih
Kopia: r-help at r-project.org
?mne: Re: [R] shorter way of coding

In exactly the same way:

y.gam <- lapply(y.list, function(y)gam(y~s(x1,x2, k=1))) # list of gam objects
y.vis.gam <- lapply(y.gam, vis.gam) # list of vis.gam outputs
y.fitted <- lapply(y.gam, fitted) # list of fitted values.

Saraj

On Tue, Dec 13, 2011 at 10:48 AM, Mintewab Bezabih
<Mintewab.Bezabih at economics.gu.se> wrote:
--
Sarah Goslee
http://www.functionaldiversity.org