[R] predict error for survreg with natural splines

Charles C. Berry cberry at tajo.ucsd.edu
Wed Dec 5 04:37:00 CET 2007


On Wed, 5 Dec 2007, Gad Abraham wrote:

> Hi,
>
> The following error looks like a bug to me but perhaps someone can shed
> light on it:
>
> > library(splines)
> > library(survival)
> > s <- survreg(Surv(futime, fustat) ~ ns(age, knots=c(50, 60)),
> data=ovarian)
> > n <- data.frame(age=rep(mean(ovarian$age), 10))
> > predict(s, newdata=n)
> Error in qr.default(t(const)) :
>   NA/NaN/Inf in foreign function call (arg 1)
>
> Thanks,
> Gad

Gad,

I think I have it now.

survreg does not automatically place the boundary knots in its $terms 
component.

You can force this by hand:


> range(ovarian$age)
[1] 38.8932 74.5041
> s <- survreg(Surv(futime, fustat) ~ ns(age, knots=c(50, 60),Boundary.knots=c(38.8932, 74.5041)),data=ovarian)
> predict(s,newdata=data.frame(age=mean(ovarian$age)))
       [,1]
1 1119.448
> all.preds <- predict(s)
> all.preds.2 <- sapply(ovarian$age,function(x) predict(s,newdata=data.frame(age=x)))
> all.equal(all.preds,all.preds.2)
[1] TRUE
>


Hard coding the boundary knots as above will assure that predict will work 
even if the data.frame used in making the fit is unavailable.

HTH,

Chuck




>
>
>
> > sessionInfo()
> R version 2.6.1 (2007-11-26)
> i486-pc-linux-gnu
>
> locale:
> LC_CTYPE=en_AU.UTF-8;LC_NUMERIC=C;LC_TIME=en_AU.UTF-8;
> LC_COLLATE=en_AU.UTF-8;LC_MONETARY=en_AU.UTF-8;LC_MESSAGES=en_AU.UTF-8;
> LC_PAPER=en_AU.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;
> LC_MEASUREMENT=en_AU.UTF-8;LC_IDENTIFICATION=C
>
> attached base packages:
> [1] splines   stats     graphics  grDevices utils     datasets  methods
> [8] base
>
> other attached packages:
> [1] survival_2.34
>
> loaded via a namespace (and not attached):
> [1] rcompgen_0.1-17
>
>
>
> -- 
> Gad Abraham
> Department of Mathematics and Statistics
> The University of Melbourne
> Parkville 3010, Victoria, Australia
> email: g.abraham at ms.unimelb.edu.au
> web: http://www.ms.unimelb.edu.au/~gabraham
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list