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 > 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
Hi Gad, The problem is with ns:> x <- ns(rnorm(100), knots=c(50, 60))Error in qr.default(t(const)) : NA/NaN/Inf in foreign function call (arg 1) but the following is OK:> x <- ns(rnorm(100)) > dim(x)[1] 100 1 Regards, Moshe. --- Gad Abraham <g.abraham at ms.unimelb.edu.au> 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 > > > > > 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. >
I think coxph() leaves safe prediction off by default. You need to turn it on. See ?predict.coxph Chuck 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 > > > > > 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
On Tue, 4 Dec 2007, Charles C. Berry wrote:> > > I think coxph() leaves safe prediction off by default. You need to turn it > on.Sorry for the misdirection, I see now that you are describing survreg() not coxph(). But even coxph() seems to barf on this. A possible workaround:> n <- data.frame(age=c(65,rep(mean(ovarian$age)), 10)) > predict(s, newdata=n)[-1][1] -0.4182318 -1.9911592>Chuck> > See > > ?predict.coxph > > Chuck > > 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 >> >> >> >> > 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 > > >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
Charles C. Berry
2007-Dec-05 01:03 UTC
[R] Another correction WAS: Re: predict error for survreg with natural splines
On Tue, 4 Dec 2007, Charles C. Berry wrote:> On Tue, 4 Dec 2007, Charles C. Berry wrote: > >> >> >> I think coxph() leaves safe prediction off by default. You need to turn it >> on. > > > Sorry for the misdirection, I see now that you are describing survreg() not > coxph(). > > But even coxph() seems to barf on this. > > A possible workaround: > >> n <- data.frame(age=c(65,rep(mean(ovarian$age)), 10)) >> predict(s, newdata=n)[-1] > [1] -0.4182318 -1.9911592This was wrong. It is not invariant to the choice of the discarded value. I guess it is time to study up on safe prediction. It is obvious I am missing something. Sorry again, Chuck>> > > Chuck > > >> >> See >> >> ?predict.coxph >> >> Chuck >> >> 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 >> > >> > >> > >> > > 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 >> >> >> > > 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 > > > >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
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, > GadGad, 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
Charles C. Berry wrote:> 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:Thanks Chuck and Moshe, manually setting the boundary fixes the problem. Cheers, Gad -- 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