Likelihood Ratio Test for Cox Models in R - r

Does anyone know of a likelihood ratio test, like lrtest in the lmtest package, that works for cox proportional hazards models produced using coxph? lrtest does not seem to work for coxph models.
Thanks

There is an anova.coxph in pkg:survival which allows comparison of model objects.
fit <- coxph(Surv(futime, fustat) ~ resid.ds *rx + ecog.ps, data = ovarian)
fit2 <- coxph(Surv(futime, fustat) ~ resid.ds +rx + ecog.ps, data=ovarian)
anova(fit2,fit)
Analysis of Deviance Table
Cox model: response is Surv(futime, fustat)
Model 1: ~ resid.ds + rx + ecog.ps
Model 2: ~ resid.ds * rx + ecog.ps
loglik Chisq Df P(>|Chi|)
1 -31.970
2 -30.946 2.0469 1 0.1525
This is an LR test.
w.r.t. the comment. A "null model" in Cox regression would be formed with only a 1 on the RHS of the formula-tilde:
fit <- coxph(Surv(futime, fustat) ~ 1, data = ovarian)

LR-Test is returned by default by coxph() from thr survival package (see last line):
require(survival)
test1 <- list(time=c(4,3,1,1,2,2,3),
status=c(1,1,1,0,1,1,0),
x=c(0,2,1,1,1,0,0),
sex=c(0,0,0,0,1,1,1))
# Fit a stratified model
coxph(Surv(time, status) ~ x + strata(sex), test1)
Call:
coxph(formula = Surv(time, status) ~ x + strata(sex), data = test1)
coef exp(coef) se(coef) z p
x 0.802 2.23 0.822 0.976 0.33
Likelihood ratio test=1.09 on 1 df, p=0.297 n= 7, number of events= 5

Related

Bootstrap for Mediation Confidence Interval [duplicate]

I have used the following function in R's coxph() to fit a cox hazard model. I want to report the proper statistics; however, there is no 95% CI in the output.
Surv(days, censor) ~ gender + age + treatment, data_1)
I only get the following columns.
coef exp(coef) se(coef) z p
A simple way to get confidence intervals for the hazard ratios associated with your predictor variables would be to use the "summary" function on your model fit. If you want confidence intervals for the coefficient estimates themselves you could use the "confint" function. Exponentiation of the results from confint can also be used to get the hazard ratio confidence intervals.
fit <- coxph(Surv(t,y) ~ x)
summary(fit) #output provides HR CIs
confint(fit) #coefficient CIs
exp(confint(fit)) #Also HR CIs
If you need this for further processing, consider a tidy solution with broom:
library(broom)
library(dplyr)
library(survival)
mod <- coxph(Surv(time, status) ~ sex + age, data = lung)
mod |>
tidy(conf.int = TRUE, exponentiate = TRUE) |>
select(term, estimate, starts_with("conf"))
#> # A tibble: 2 x 4
#> term estimate conf.low conf.high
#> <chr> <dbl> <dbl> <dbl>
#> 1 sex 0.599 0.431 0.831
#> 2 age 1.02 0.999 1.04
More details at this reference
You can simply find CI conf.int in CoxPH model summary in R ( in case using survival package . Its under lower .95 and upper .95
Update 2019 (But I assume the suggestion did not work previously):
test1 <- list(time=c(4,3,1,1,2,2,3),
status=c(1,1,1,0,1,1,0),
x=c(0,2,1,1,1,0,0),
sex=c(0,0,0,0,1,1,1))
# Fit a stratified model
coxobj <- coxph(Surv(time, status) ~ x + strata(sex), test1)
coxobj_summary <- summary(coxobj)
coxobj_summary$conf.int
Output:
exp(coef) exp(-coef) lower .95 upper .95
x 2.230706 0.4482887 0.4450758 11.18022

Extracting linear predictors from general linear model for panel data ( pglm )

I would like to extract linear predictors out of a pglm model.
With a basic glm model, it can be done simply using the command $linear.predictors
#For example
library(plm)
library(pglm)
data(UnionWage) # from pglm-package
punions <- pdata.frame(UnionWage, c("id", "year"))
punions <- subset(punions, wage > 0)
glm.model <- glm(wage ~ exper + rural + married, data=punions, family = "poisson")
glm.model$linear.predictors
How can I extract or compute linear predictors from a pglm model like
pglm.model <- pglm(wage ~ exper + rural + married, data=punions, model="random", family="poisson")
I hope anybody can help!

how to config nlme to give the same results as lme

The question looks naive but I am puzzled with the configuration of the nlme function in R to get equivalent results to a given lme model.
This appears to work. Note that the defaults for method are different for lme ("REML") and nlme ("ML") ...
m1 <- lme(distance ~ age,
random = ~ age |Subject, data=Orthodont,
method="ML")
nlme requires starting values - cheat here and use those from lme:
m2 <- nlme(distance ~ mu,
fixed = mu ~ age,
random = mu ~ age | Subject,
data=Orthodont,
start=list(fixed=fixef(m1)))
The variance-covariance matrices are almost identical.
> VarCorr(m1)
Subject = pdLogChol(age)
Variance StdDev Corr
(Intercept) 4.81407327 2.1940996 (Intr)
age 0.04619252 0.2149244 -0.581
Residual 1.71620466 1.3100399
> VarCorr(m2)
Subject = pdLogChol(list(mu ~ age))
Variance StdDev Corr
mu.(Intercept) 4.81408901 2.1941032 m.(In)
mu.age 0.04619255 0.2149245 -0.581
Residual 1.71620373 1.3100396

How to get 95% CI from R's coxph?

I have used the following function in R's coxph() to fit a cox hazard model. I want to report the proper statistics; however, there is no 95% CI in the output.
Surv(days, censor) ~ gender + age + treatment, data_1)
I only get the following columns.
coef exp(coef) se(coef) z p
A simple way to get confidence intervals for the hazard ratios associated with your predictor variables would be to use the "summary" function on your model fit. If you want confidence intervals for the coefficient estimates themselves you could use the "confint" function. Exponentiation of the results from confint can also be used to get the hazard ratio confidence intervals.
fit <- coxph(Surv(t,y) ~ x)
summary(fit) #output provides HR CIs
confint(fit) #coefficient CIs
exp(confint(fit)) #Also HR CIs
If you need this for further processing, consider a tidy solution with broom:
library(broom)
library(dplyr)
library(survival)
mod <- coxph(Surv(time, status) ~ sex + age, data = lung)
mod |>
tidy(conf.int = TRUE, exponentiate = TRUE) |>
select(term, estimate, starts_with("conf"))
#> # A tibble: 2 x 4
#> term estimate conf.low conf.high
#> <chr> <dbl> <dbl> <dbl>
#> 1 sex 0.599 0.431 0.831
#> 2 age 1.02 0.999 1.04
More details at this reference
You can simply find CI conf.int in CoxPH model summary in R ( in case using survival package . Its under lower .95 and upper .95
Update 2019 (But I assume the suggestion did not work previously):
test1 <- list(time=c(4,3,1,1,2,2,3),
status=c(1,1,1,0,1,1,0),
x=c(0,2,1,1,1,0,0),
sex=c(0,0,0,0,1,1,1))
# Fit a stratified model
coxobj <- coxph(Surv(time, status) ~ x + strata(sex), test1)
coxobj_summary <- summary(coxobj)
coxobj_summary$conf.int
Output:
exp(coef) exp(-coef) lower .95 upper .95
x 2.230706 0.4482887 0.4450758 11.18022

standard error of outcome in lm and lme

I have the following linear models
library(nlme)
fm2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
fm2.lm <- lm(distance ~ age + Sex,data = Orthodont)
How can I obtain the standard error of distance with age and Sex?
For fm2 (linear mixed model), you can do
sqrt(diag(summary(fm2)$varFix))
#(Intercept) age SexFemale
# 0.83392247 0.06160592 0.76141685
For fm2.lm (linear model), you can do
summary(fm2.lm)$coefficients[, "Std. Error"]
#(Intercept) age SexFemale
# 1.11220946 0.09775895 0.44488623
see attributes(summary(your.model)). what you're after is summary(your.model)$coefficients (or did I get your question wrong?). just use subsetting with [] to get what you want

Resources