GAMs in R : How to put the new data into the model?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

GAMs in R : How to put the new data into the model?

pigpigmeow
I have 5  GAMs ( model1, model2, model3, model4 and model5)
Before I use some data X(predictor -January to June data) to form a equation and calculate the expected value of Y (predictand -January to June). After variable selection, GAMs (Model 1)were bulit up! R-square :0.40

NOW, I want to use new X'( predictor -July - December data) and put into Model 1, then get the expected value of Y' (predictand-July -December)

I used mcgv package, what should I do now?
Reply | Threaded
Open this post in threaded view
|

Re: GAMs in R : How to put the new data into the model?

Daniel Malter
Your questions is pretty opaque. Please adhere to the posting guide. Provide a self-contained (!) example (i.e., code) that reproduces your problem. Generally, you would predict like this:

x<-rnorm(100)
e<-rnorm(100)
y<-x+x^2+e
reg<-gam(y~s(x))
plot(reg)

predict(reg,newdata=data.frame(x=2))

where you can substitute any vector of values for x

HTH,
Daniel


pigpigmeow wrote
I have 5  GAMs ( model1, model2, model3, model4 and model5)
Before I use some data X(predictor -January to June data) to form a equation and calculate the expected value of Y (predictand -January to June). After variable selection, GAMs (Model 1)were bulit up! R-square :0.40

NOW, I want to use new X'( predictor -July - December data) and put into Model 1, then get the expected value of Y' (predictand-July -December)

I used mcgv package, what should I do now?
Reply | Threaded
Open this post in threaded view
|

Re: GAMs in R : How to put the new data into the model?

pigpigmeow
For example:
GAMs and after stepwise regression:
cod<-gam(newCO~RH+s(solar,bs="cr")+windspeed+s(transport,bs="cr"),family=gaussian (link=log),groupD,methods=REML)

I used 10 year meterorology data (2000-2010) to form equation of concentration of carbon monoxide.
NOW, I have 2011 meteorology data, I want to use the above GAMs to get the predict value of concentration of carbon monoxide.

How can I put the 2011 data into this gam and get the expected value of concentration of Carbon monoxide??
Reply | Threaded
Open this post in threaded view
|

Re: GAMs in R : How to put the new data into the model?

Simon Wood-4
See ?predict.gam

You create a data frame (e.g. `dat2011') containing the 2011 values for
RH, solar, windspeed and transport, then

predict(cod,dat2011,type="response")

there are various options for type -- see ?predict.gam



On 09/28/2011 10:10 AM, pigpigmeow wrote:

> For example:
> GAMs and after stepwise regression:
> cod<-gam(newCO~RH+s(solar,bs="cr")+windspeed+s(transport,bs="cr"),family=gaussian
> (link=log),groupD,methods=REML)
>
> I used 10 year meterorology data (2000-2010) to form equation of
> concentration of carbon monoxide.
> NOW, I have 2011 meteorology data, I want to use the above GAMs to get the
> predict value of concentration of carbon monoxide.
>
> How can I put the 2011 data into this gam and get the expected value of
> concentration of Carbon monoxide??
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/GAMs-in-R-How-to-put-the-new-data-into-the-model-tp3849851p3850473.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: GAMs in R : How to put the new data into the model?

Gavin Simpson
In reply to this post by pigpigmeow
On Wed, 2011-09-28 at 02:10 -0700, pigpigmeow wrote:
> For example:
> GAMs and after stepwise regression:

You probably don't want to be doing stepwise model/feature selection in
any regression model. Marra & Wood (2011, Computational Statistics and
Data Analysis 55; 2372-2387) show results that suggest adding an extra
penalty term during model fitting, that allows terms to be penalised out
of the model, performs well for feature selection in a series of GAM
exercises.

You can turn this on in mgcv::gam() via the `select` argument by setting
it to `TRUE`.

> cod<-gam(newCO~RH+s(solar,bs="cr")+windspeed+s(transport,bs="cr"),family=gaussian
> (link=log),groupD,methods=REML)
>
> I used 10 year meterorology data (2000-2010) to form equation of
> concentration of carbon monoxide.
> NOW, I have 2011 meteorology data, I want to use the above GAMs to get the
> predict value of concentration of carbon monoxide.

So put your 2011 data into a data frame with (at the minimum) the column
names:

`RH`, `solar`, `windspeed`, `transport`

If that data frame were called, say, `dat2011` then you provide the
`predict()` method for "gam" objects with both the fitted model **and**
the new data (`dat2011`), e.g.:

pred <- predict(cod, newdata = dat2011)

See ?predict.gam for more details. You are probably going to need type =
"response" so you get values back on the scale of the response not the
the link function.

HTH

G

> How can I put the 2011 data into this gam and get the expected value of
> concentration of Carbon monoxide??
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/GAMs-in-R-How-to-put-the-new-data-into-the-model-tp3849851p3850473.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
[hidden email] 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.