[R] Formula parsing and updating

Charles C. Berry cberry at tajo.ucsd.edu
Fri Dec 5 01:21:41 CET 2008


On Thu, 4 Dec 2008, Johannes Huesing wrote:

> Hi all,
> I can't come over a problem with formula. Suppose I have a coxmod model
> with the following formula:
>
>> somemod$formula
> Surv(lebzeit, tot == 1) ~ sex + (alter >= 65) + diff3k + zelltyp_k_c +
>    q_nuc_3k + kar_k80_g80 + stadium

You need to give us something to reproduce. I get an error when I try

> example(coxph)
> test1$quant <- rnorm(length(test1$sex))
> fit <- coxph( Surv(time, status) ~ x + (quant>0) + strata(sex), test1)
Error in model.matrix.default(newTerms, m) :
   model frame and formula mismatch in model.matrix()

so I do not see how you obtained 'somemod'.

When I protect with I(quant>0), I have no problems obtaining 'fit' nor 
update()ing it:

> fit <- coxph( Surv(time, status) ~ x + I(quant>0) + strata(sex), test1)
> update(fit,".~.-x")
Call:
coxph(formula = Surv(time, status) ~ I(quant > 0) + strata(sex),
     data = test1)


                    coef exp(coef) se(coef)      z    p
I(quant > 0)TRUE -0.746     0.474     1.27 -0.589 0.56

Likelihood ratio test=0.37  on 1 df, p=0.545  n=6 (1 observation deleted 
due to missingness)


HTH,

Chuck

>
> and I want to drop the stadium explanatory variable from the model with
>
>> update(somemod, ". ~ . - stadium")
>
> I get the following messages:
>
> Fehler in coxph(formula = Surv(lebzeit, tot == 1) ~ sex + alter >= 65 +  :
>  No (non-missing) observations
> Zusätzlich: Warning messages:
> 1: In Ops.factor(sex, alter) : + not meaningful for factors
> 2: In Ops.factor(65, diff3k) : + not meaningful for factors
> 3: In Ops.factor(65 + diff3k, zelltyp_k_c) : + not meaningful for factors
> 4: In Ops.factor(65 + diff3k + zelltyp_k_c, q_nuc_3k) :
>  + not meaningful for factors
> 5: In Ops.factor(65 + diff3k + zelltyp_k_c + q_nuc_3k, kar_k80_g80) :
>  + not meaningful for factors
>
> What seems to happen is that "alter >= 65" is not parsed as an entity.
>
> It does not look as a problem with the environment to me, as is outlined
> in http://developer.r-project.org/nonstandard-eval.pdf, rather a parsing
> problem.
>
> By contrast, the following works like a charm:
>
>> x <- runif(50)
>> y <- rnorm(50)+x
>> regmod <- lm(y ~ x)
>> update(regmod, " . ~ . + (ifelse(x>.5, 0, x))")
>
> Could anybody give me a hint on my wrongdoings?
>
> -- 
> Johannes Hüsing               There is something fascinating about science.
>                              One gets such wholesale returns of conjecture
> mailto:johannes at huesing.name  from such a trifling investment of fact.
> http://derwisch.wikidot.com         (Mark Twain, "Life on the Mississippi")
>
> ______________________________________________
> 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