Skip to content

Instantly share code, notes, and snippets.

@alexhanna
Last active December 15, 2015 09:19
Show Gist options
  • Save alexhanna/5237349 to your computer and use it in GitHub Desktop.
Save alexhanna/5237349 to your computer and use it in GitHub Desktop.
Model fit with residuals
t.cox2_ph <- coxph(t.surv ~ (Age + PlusSize + PuertoRico + Wins + Highs + Lows + Lipsyncs + CompLeft +
Wins*CompLeft + Highs*CompLeft + Lows*CompLeft + Lipsyncs*CompLeft) + cluster(ID), df)
t.cox3s <- coxph(t.surv ~ (Age + PlusSize + PuertoRico + Wins + Highs + Lows + LipsyncWithoutOut + CompLeft) + cluster(ID), df)
model.df <- data.frame(ID = integer(0), Residuals = double(0), Model = character(0))
model.list <- list(c2 = t.cox2, c2ph = t.cox2_ph, c3 = t.cox3, c3s = t.cox3s)
for (i in 1:length(model.list)) {
name <- names(model.list[i])
cMod <- model.list[[i]]
class <- substr(name, 0, 2)
cMod.res <- residuals(cMod, type = "deviance", collapse = df$ID)
cMod.df <- data.frame(ID = as.numeric(names(cMod.res)), Residuals = as.numeric(cMod.res), Model = name, Class = class)
cMod.mse <- sum( cMod.res^2 ) / length(cMod.res)
model.df <- rbind(model.df, cMod.df)
print(cMod.mse)
}
p <- ggplot(model.df, aes(ID, Residuals, color = factor(Model) ))
p <- p + geom_point() + geom_smooth( fill = NA )
p <- p + geom_vline( xintercept = c(9.5, 21.5, 34.5), linetype = 3 )
p <- p + geom_hline( yintercept = 0, alpha = 0.5 )
p <- p + scale_color_discrete("Model")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment