[R] How to define degree=1 in mgcv

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Jan 25 13:05:46 CET 2010


On Sun, 2010-01-24 at 16:28 -0500, hellen lee wrote:
> Hi, all
> 
> I have a question on mgcv and ns. Now I want to compare the results from
> glm, gam and ns. Take a simple model y~x for example.
> 
> glm1 = glm(y~x, data=data1)
> gam1 = gam(y~s(x), data=data1)
> ns1 = glm(y~ns(x),data=data1)
> 
> In order to confirm the result from glm1 is consistent to those from gam1
> and ns1, I want to define degree=1 in mgcv and ns. I am wondering if there
> is somebody can give me some suggestions? I appreciate.

require(mgcv)
require(splines)
data(airquality)

m1 <- glm(Ozone ~ Wind, data = airquality)
m2 <- gam(Ozone ~ Wind, data = airquality)
m3 <- glm(Ozone ~ ns(Wind, df = 1), data = airquality)

For gam, I don't think you can set a smooth to be linear, which is what
I interpret degree = 1 to be (I also don't know where you get degree = 1
from, this isn't anything to do with ns() in package splines). So we
just don't specify that Wind be a smoother in the GAM --- not sure if
that is what you wanted?

Compare the fits using say summary:

e.g. summary(m1); summary(m2)

Also, from the above, note that these are all linear models (as opposed
to generalised) and would be fitted more efficiently using lm():

m4 <- lm(Ozone ~ Wind, data = airquality)
m5 <- lm(Ozone ~ ns(Wind, df = 1), data = airquality)

> all.equal(fitted(m1), fitted(m2))
[1] TRUE
> all.equal(fitted(m1), fitted(m3))
[1] TRUE
> all.equal(fitted(m1), fitted(m4))
[1] TRUE
> all.equal(fitted(m1), fitted(m5))
[1] TRUE

HTH

G

> 
> Hellen
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list