[R] Do YOU know an equation for splines (ns)?

Spencer Graves spencer.graves at structuremonitoring.com
Thu Jun 7 01:19:50 CEST 2012


       I agree with Bill and Bert:  "predict" is the proper tool for 
making predictions.  Pinheiro and Bates (2000) Mixed-Effects Models in S 
and S-Plus (Springer) includes several entries in the index for 
"predictions".  Please note, however, that there are a few lines of code 
in that book the work in S-Plus but not R.  Fortunately, the corrections 
are available in script files distributed with the package, which you 
can find as follows:


> system.file('scripts', package='nlme')
[1] "c:/Program Files/R/R-2.15.0/library/nlme/scripts"


       The TaylorSpline{fda} function will give you explicit 
coefficients each segment of a spline.  However, if you want model 
predictions, you are probably best using predict with objects produced 
by functions in nlme.  That package has seen lots of use and attention 
by the R Core team, and should be pretty good -- especially with the 
documentation provided by Pinheiro and Bates.


       Hope this helps.
       Spencer


On 6/6/2012 1:48 PM, William Dunlap wrote:
> Do you have to include the grouping variable, plotF, in your newdata
> argument?  E.g., after fitting the model with
>    rcn10G<-groupedData(N ~ day | plotF, data=rcn10)
>    fit10<- lme( N~ns(day, 3), data = rcn10G)
> try checking the predictions when you've include plotF in newdata:
>    par(mfrow=c(2,1))
>    plot(N ~ day, subset=plotF=="12", data=rcn10G)
>    points(num, predict(fit10, data.frame(day=num, plotF=rep("12", length(num)))), pch=".", col="red")
>
>    plot(N ~ day, subset=plotF=="43", data=rcn10G)
>    points(num, predict(fit10, data.frame(day=num, plotF=rep("43", length(num)))), pch=".", col="red")
>
> I am no expert on the lme and groupedData, but the general rule is that all variables involved
> in the model, except the response, must be given to predict.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
>> Of Ranae
>> Sent: Wednesday, June 06, 2012 10:39 AM
>> To: r-help at r-project.org
>> Subject: Re: [R] Do YOU know an equation for splines (ns)?
>>
>> I have not been able to get "predict" (or most functions) to run well with
>> grouped data in nlme.  I may not have it coded right, but this is what it
>> looks like:
>>
>> http://r.789695.n4.nabble.com/file/n4632567/spline.txt spline.txt
>>
>> library(nlme)
>> library(splines)
>>
>> rootCN<-read.table("spline.txt", header=TRUE)
>> rootCN$plotF<-as.factor(rootCN$plot)
>>
>> rcn10G<-groupedData(N ~ day | plotF, data=rcn10)
>>
>> fit10<- lme( N~ns(day, 3), data = rcn10G)
>>
>> plot(augPred(fit10))
>>
>> num<- seq(88,300, len=200)
>> lines(num, predict(fit10, data.frame(day=num)))
>>
>> -Ranae
>>
>>
>> Does
>>   ?predict.ns
>>   not do what you want without having to explicitly manipulate the spline
>> basis?
>>
>> -- Bert
>>
>> On Tue, Jun 5, 2012 at 1:56 PM, Ranae<[hidden email]>  wrote:
>>
>>> Hi,
>>>
>>> I am looking at the change in N concentration in plant roots over 4 time
>>> points and I have fit a spline to the data using ns and lme:
>>>
>>> fit10<- lme( N~ns(day, 3), data = rcn10G)
>>>
>>> I may want to adjust the model a little bit, but for now, let's assume
>>> it's
>>> good.  I get output for the fixed effects:
>>>
>>> Fixed: N ~ ns(day, 3)
>>> (Intercept) ns(day, 3)1 ns(day, 3)2 ns(day, 3)3
>>>   1.15676524  0.14509171  0.04459627  0.09334428
>>>
>>> and coefficients for each experimental unit in my experiment:
>>>
>>>    (Intercept) ns(day, 3)1 ns(day, 3)2 ns(day, 3)3
>>> 24    1.050360 -0.42666159 -0.56290877 -0.10714407
>>> 13    1.104464 -0.30825350 -0.53311653 -0.05558150
>>> 31    1.147878 -0.14548512 -0.78673906 -0.07231781
>>> 46    1.177781 -0.22278380 -0.80278177 -0.02321460
>>> 15    1.144215 -0.04484519 -0.06084798  0.07633663
>>> 32    1.213007  0.00741061  0.03896933  0.15325849
>>> 23    1.274615  0.16477514  0.00872224  0.23128320
>>> 41    1.215626  0.57050767  0.11415467  0.10608867
>>> 43    1.134203  0.48070741  0.72112899  0.18108193
>>> 12    1.091422  0.39563632  1.01521528  0.22597459
>>> 21    1.100631  0.44589314  0.98526322  0.23535739
>>> 35    1.226980  0.82419937  0.39809568  0.16900841
>>>
>>> NOW, I want to write a spline function where I can incorporate these
>>> coefficients to get the predicted N concentration value for each day.
>>> However, I am having trouble finding the right spline equation, since
>>> there
>>> are many forms on the internets.
>>>
>>> I know it won't be a simple one, but can some one direct me to the
>>> equation
>>> that would be best to use for ns?
>>>
>>> Thanks a lot,
>>>
>>> Ranae
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Do-YOU-know-an-
>> equation-for-splines-ns-tp4632440p4632567.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
> ______________________________________________
> 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.
>


-- 
Spencer Graves, PE, PhD
President and Chief Technology Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567
web:  www.structuremonitoring.com



More information about the R-help mailing list