From ripley at stats.ox.ac.uk  Tue Feb  9 11:05:34 2010
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 9 Feb 2010 10:05:34 +0000 (GMT)
Subject: [Rd] lm combined with splines
In-Reply-To: <4A1BF5B503CF294C88F65AE7ECF5754621F3367F07@exmbx02.intranet.chuv>
References: <4A1BF5B503CF294C88F65AE7ECF5754621F3367F07@exmbx02.intranet.chuv>
Message-ID: <alpine.LFD.2.00.1002090951350.7535@localhost>

This is really a question for R-help: it is not about R development 
nor software development in R.

The answer to the only question I see is simple: your model depends on 
'fb3' and you supplied 'height'.

On Tue, 9 Feb 2010, Randriamiharisoa Alex wrote:

> Hello,

> In the following I tried 3 versions of an example in R help. Only 
> the two first predict command work.

Which 'example in R help'?  If you mean that from ?bs, then the 
comment about 'safe prediction' is why your third approach (even if 
done something like

ph3 <- predict(fm3, data.frame(fb3 = I(bs(ht3, df = 5))))

) would not be correct.  You need to predict using the spline with the 
knots computed by the original set of data, and predict() is smart 
enough to figure that out when it sees a formula involving bs() (look 
at str(fm1) to see where the information is stored, and hence how you 
could mimic this).


> After :
>
> library(splines)
> require(stats)
>
> 1)
> fm1 <- lm(weight ~ bs(height, df = 5), data = women)
> ht1  <- seq(57, 73, len = 200)
> ph1  <- predict(fm1, data.frame(height=ht1))  # OK
> plot(women, xlab = "Height (in)", ylab = "Weight (lb)")
> lines(ht1, ph1)
>
> 2)
> height <- women$height        # 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
> weight <- women$weight        # 115 117 120 123 126 129 132 135 139 142 146 150 154 159 164
>
> fm2 <- lm(weight ~ bs(height, df = 5))
> ht2 <- seq(57, 73, len = 200)
> ph2 <- predict(fm2, data.frame(height=ht2)) # OK
> plot(women, xlab = "Height (in)", ylab = "Weight (lb)")
> lines(ht2,ph2)
>
> 3)
> height <- women$height        # 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
> weight <- women$weight        # 115 117 120 123 126 129 132 135 139 142 146 150 154 159 164
>
> fb3 <- bs(height, df = 5)
> fm3 <- lm(weight ~ fb3)
> ht3 <- seq(57, 73, len = 200)
> ph3 <- predict(fm3, data.frame(height=ht3))  # Error message about newdata. Why ?
> plot(women, xlab = "Height (in)", ylab = "Weight (lb)")
> lines(ht3,ph3) # no line
>
> Thanks for the reason of this message.
> Alex Randria
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] lm combined with splines
Prof Brian Ripley