daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Feb 14th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Needed libraries
  2. library(ggplot2)
  3. library(tidyverse)
  4. library(purrr)
  5. library(broom)
  6. library(survival)
  7. #Create data set
  8. mpg_data <- mpg
  9. mpg_data <- mpg_data %>%
  10.   mutate(mpg_diff = cty - hwy)
  11. mpg_data <- mpg_data %>%
  12.   mutate(EVENT = (mpg_diff >= -8))
  13. set.seed(1)
  14. mpg_data <- mpg_data %>%
  15.   mutate(TIME_TO_EVENT = as.integer(runif(234, 1, 100)))
  16. mpg_nested <- mpg_data %>%
  17.   group_by(manufacturer) %>%
  18.   mutate(n_prot = length(model)) %>%
  19.   nest()
  20. # Stepwise regression
  21. stepwise <- function(data) {
  22.   response <- Surv(time = data$TIME_TO_EVENT, event = data$EVENT, type = "right")
  23. full <- "Surv(time = data$TIME_TO_EVENT, event = data$EVENT, type = 'right') ~ data$cyl+data$cty+data$hwy+data$displ"
  24. x <- factor(as.factor(data$model))
  25. full <- ifelse(nlevels(x) >= 2, paste(full, "as.character(data$model)", sep = "+"), full)
  26. x <- factor(as.factor(data$trans))
  27. full <- ifelse(nlevels(x) >= 2, paste(full, "as.character(data$trans)", sep = "+"), full)
  28. x <- factor(as.factor(data$drv))
  29. full <- ifelse(nlevels(x) >= 2, paste(full, "as.character(data$drv)", sep = "+"), full)
  30. null_model_ONE <- coxph(response ~ 1, data=data)
  31. full_model_ONE <- coxph(as.formula(full), data=data)
  32. model_ONE <- step(null_model_ONE, scope=list(lower=null_model_ONE, upper=full_model_ONE))
  33. }
  34. survival_mpg <- mpg_nested %>%  
  35.   mutate(model_fit = map(data, stepwise))
  36. #Predicting values
  37. #This works but is not type="expected"
  38. survival_mpg_predict <- survival_mpg %>%
  39.   mutate(mpg_predict = map2(model_fit, data, predict))
  40. ##TRY 1##
  41. predict.F <- function(model_fit, data){
  42.   predict(model_fit, newdata=data, type="expected")
  43. }
  44. survival_mpg_predict <- survival_mpg %>%
  45.   mutate(mpg_predict = map2(model_fit, data, predict.F))
  46. #Error in mutate_impl(.data, dots) : Evaluation error: requires numeric/complex matrix/vector arguments.
  47. ##Try 2##
  48. survival_mpg_predict <- survival_mpg %>%
  49.   mutate(mpg_predict = map2(model_fit, data, predict(model_fit, newdata = data, type="expected")))
  50. #Error in mutate_impl(.data, dots) : Evaluation error: no applicable method for 'predict' applied to an object of class "list".
  51. ##Try 3##
  52. survival_mpg_predict <- survival_mpg %>%
  53.   mutate(mpg_predict = map2(model_fit, data, ~ predict(.x, newdata = .y, type="expected")))
  54. #Error in mutate_impl(.data, dots) : Evaluation error: requires numeric/complex matrix/vector arguments.
  55. ##Try 4##
  56. survival_mpg_predict <- survival_mpg %>%
  57.   mutate(mpg_predict = map2(model_fit, data, function(model_fit, data) predict(model_fit, newdata=data, type="expected")))
  58. #Error in mutate_impl(.data, dots) : Evaluation error: requires numeric/complex matrix/vector arguments.
  59.    
  60. predict.F <- function(model_fit, data){
  61. predict(model_fit, type="expected")
  62. }
  63. survival_mpg_predict <- survival_mpg %>%
  64. mutate(mpg_predict = map(model_fit, predict.F))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top