Guest User

Untitled

a guest
Jan 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. set.seed(10)
  2. intercept <- 0
  3. beta1 <- 0.5
  4. beta2 <- 1
  5. n = 1000
  6. xtest <- rnorm(n,1,1)
  7. gender <- factor(rbinom(n, 1, .4), labels=c("Male", "Female"))
  8. random_noise <- runif(n, -1,1)
  9.  
  10. # Add a ceiling and a floor to simulate a bound score
  11. fake_ceiling <- 4
  12. fake_floor <- -1
  13.  
  14. # Just to give the graphs the same look
  15. my_ylim <- c(fake_floor - abs(fake_floor)*.25,
  16. fake_ceiling + abs(fake_ceiling)*.25)
  17. my_xlim <- c(-1.5, 3.5)
  18.  
  19. # Simulate the predictor
  20. linpred <- intercept + beta1*xtest^3 + beta2*(gender == "Female") + random_noise
  21. # Remove some extremes
  22. linpred[linpred > fake_ceiling + abs(diff(range(linpred)))/2 |
  23. linpred < fake_floor - abs(diff(range(linpred)))/2 ] <- NA
  24. #limit the interval and give a ceiling and a floor effect similar to scores
  25. linpred[linpred > fake_ceiling] <- fake_ceiling
  26. linpred[linpred < fake_floor] <- fake_floor
  27.  
  28. library(ggplot2)
  29. # Just to give all the graphs the same look
  30. my_ylim <- c(fake_floor - abs(fake_floor)*.25,
  31. fake_ceiling + abs(fake_ceiling)*.25)
  32. my_xlim <- c(-1.5, 3.5)
  33. qplot(y=linpred, x=xtest, col=gender, ylab="Outcome")
  34.  
  35. library(rms)
  36.  
  37. # Regular linear regression
  38. fit_lm <- Glm(linpred~rcs(xtest, 5)+gender, x=T, y=T)
  39. boot_fit_lm <- bootcov(fit_lm, B=500)
  40. p <- Predict(boot_fit_lm, xtest=seq(-2.5, 3.5, by=.001), gender=c("Male", "Female"))
  41. lm_plot <- plot.Predict(p,
  42. se=T,
  43. col.fill=c("#9999FF", "#BBBBFF"),
  44. xlim=my_xlim, ylim=my_ylim)
  45.  
  46. # Quantile regression regular
  47. fit_rq <- Rq(formula(fit_lm), x=T, y=T)
  48. boot_rq <- bootcov(fit_rq, B=500)
  49. # A little disturbing warning:
  50. # In rq.fit.br(x, y, tau = tau, ...) : Solution may be nonunique
  51.  
  52. p <- Predict(boot_rq, xtest=seq(-2.5, 3.5, by=.001), gender=c("Male", "Female"))
  53. rq_plot <- plot.Predict(p,
  54. se=T,
  55. col.fill=c("#9999FF", "#BBBBFF"),
  56. xlim=my_xlim, ylim=my_ylim)
  57.  
  58. # The logit transformations
  59. logit_fn <- function(y, y_min, y_max, epsilon)
  60. log((y-(y_min-epsilon))/(y_max+epsilon-y))
  61.  
  62.  
  63. antilogit_fn <- function(antiy, y_min, y_max, epsilon)
  64. (exp(antiy)*(y_max+epsilon)+y_min-epsilon)/
  65. (1+exp(antiy))
  66.  
  67.  
  68. epsilon <- .0001
  69. y_min <- min(linpred, na.rm=T)
  70. y_max <- max(linpred, na.rm=T)
  71. logit_linpred <- logit_fn(linpred,
  72. y_min=y_min,
  73. y_max=y_max,
  74. epsilon=epsilon)
  75.  
  76. fit_rq_logit <- update(fit_rq, logit_linpred ~ .)
  77. boot_rq_logit <- bootcov(fit_rq_logit, B=500)
  78.  
  79.  
  80. p <- Predict(boot_rq_logit, xtest=seq(-2.5, 3.5, by=.001), gender=c("Male", "Female"))
  81.  
  82. # Change back to org. scale
  83. transformed_p <- p
  84. transformed_p$yhat <- antilogit_fn(p$yhat,
  85. y_min=y_min,
  86. y_max=y_max,
  87. epsilon=epsilon)
  88. transformed_p$lower <- antilogit_fn(p$lower,
  89. y_min=y_min,
  90. y_max=y_max,
  91. epsilon=epsilon)
  92. transformed_p$upper <- antilogit_fn(p$upper,
  93. y_min=y_min,
  94. y_max=y_max,
  95. epsilon=epsilon)
  96.  
  97. logit_rq_plot <- plot.Predict(transformed_p,
  98. se=T,
  99. col.fill=c("#9999FF", "#BBBBFF"),
  100. xlim=my_xlim, ylim=my_ylim)
  101.  
  102. library(lattice)
  103. # Calculate the true lines
  104. x <- seq(min(xtest), max(xtest), by=.1)
  105. y <- beta1*x^3+intercept
  106. y_female <- y + beta2
  107. y[y > fake_ceiling] <- fake_ceiling
  108. y[y < fake_floor] <- fake_floor
  109. y_female[y_female > fake_ceiling] <- fake_ceiling
  110. y_female[y_female < fake_floor] <- fake_floor
  111.  
  112. tr_df <- data.frame(x=x, y=y, y_female=y_female)
  113. true_line_plot <- xyplot(y + y_female ~ x,
  114. data=tr_df,
  115. type="l",
  116. xlim=my_xlim,
  117. ylim=my_ylim,
  118. ylab="Outcome",
  119. auto.key = list(
  120. text = c("Male"," Female"),
  121. columns=2))
  122.  
  123.  
  124. # Just for making pretty graphs with the comparison plot
  125. compareplot <- function(regr_plot, regr_title, true_plot){
  126. print(regr_plot, position=c(0,0.5,1,1), more=T)
  127. trellis.focus("toplevel")
  128. panel.text(0.3, .8, regr_title, cex = 1.2, font = 2)
  129. trellis.unfocus()
  130. print(true_plot, position=c(0,0,1,.5), more=F)
  131. trellis.focus("toplevel")
  132. panel.text(0.3, .65, "True line", cex = 1.2, font = 2)
  133. trellis.unfocus()
  134. }
  135.  
  136. compareplot(lm_plot, "Linear regression", true_line_plot)
  137. compareplot(rq_plot, "Quantile regression", true_line_plot)
  138. compareplot(logit_rq_plot, "Logit - Quantile regression", true_line_plot)
  139.  
  140. > contrast(boot_rq_logit, list(gender=levels(gender),
  141. + xtest=c(-1:1)),
  142. + FUN=function(x)antilogit_fn(x, epsilon))
  143. gender xtest Contrast S.E. Lower Upper Z Pr(>|z|)
  144. Male -1 -2.5001505 0.33677523 -3.1602179 -1.84008320 -7.42 0.0000
  145. Female -1 -1.3020162 0.29623080 -1.8826179 -0.72141450 -4.40 0.0000
  146. Male 0 -1.3384751 0.09748767 -1.5295474 -1.14740279 -13.73 0.0000
  147. * Female 0 -0.1403408 0.09887240 -0.3341271 0.05344555 -1.42 0.1558
  148. Male 1 -1.3308691 0.10810012 -1.5427414 -1.11899674 -12.31 0.0000
  149. * Female 1 -0.1327348 0.07605115 -0.2817923 0.01632277 -1.75 0.0809
  150.  
  151. Redundant contrasts are denoted by *
  152.  
  153. Confidence intervals are 0.95 individual intervals
Add Comment
Please, Sign In to add comment