welcome: please sign in
location: attachment:session10exer.r von RstatisTik/RstatisTikPortal/RcourSe/RcourseJune

Dateianhang 'session10exer.r'

Herunterladen

   1 load("birthsweights.rdata")
   2 
   3 ## Exercises
   4 ## Remember: We used hypertension of the mom to explain variation
   5 ## in the birth weight of the kid. Without looking in the material
   6 ## of the last session, try to redo the model.
   7 
   8 
   9 
  10 
  11 
  12 require(effects)
  13 
  14 
  15 
  16 
  17 res <- allEffects(m.hyp, se = T)
  18 summary(res)
  19 
  20 invlogit <- function(x){
  21     exp(x)/(1+exp(x))
  22 }
  23 
  24 
  25 ## What is the relationship between the coefficients of the model
  26 ## (from the model summary) and the effects?
  27 
  28 
  29 
  30 
  31 ## binomial/logistic regression
  32 m.wks <- glm(lowbw ~ gestwks, family=binomial, data=births)
  33 summary(m.wks)
  34 
  35 
  36 ## interpreting the coefficients
  37 invlogit(coef(m.wks)[1])
  38 
  39 exp(coef(m.wks)[2])
  40 
  41 
  42 ## effects
  43 Effect("gestwks",m.wks)
  44 
  45 
  46 ## Exercise
  47 Effect("gestwks",m.wks,xlevels = list(gestwks = c(20,30,40)))
  48 
  49 
  50 ## visualiziation
  51 require(ggplot2)
  52 ggplot(births,aes(x = gestwks, y = as.numeric(lowbw)-1)) +
  53     geom_smooth(method = "glm", family = "binomial",se = F,size = 2) +
  54     geom_point(shape="|")
  55 
  56 
  57 ## Exercises
  58 
  59 ## try to change the axis titles (xlab() and ylab())
  60 ## add a title (ggtitle())
  61 
  62 
  63 
  64 
  65 
  66 
  67 ## change the colour of the function to black
  68 ## change the colour of the points to red for the low birth weight
  69 ## and to green for the one with normal birth weight
  70 
  71 
  72 
  73 
  74 
  75 
  76 
  77 ## change the position of the legend;
  78 ## place it somewhere near the upper right corner
  79 ## inside the plotting area
  80 
  81 
  82 
  83 
  84 
  85 
  86 
  87 ## the famous Challenger DAta
  88 
  89 require(faraway)
  90 data(orings)
  91 
  92 ## model
  93 m.oring <- glm(cbind(damage,6-damage) ~ temp,family=binomial, orings)
  94 summary(m.oring)
  95 
  96 ## coefficients
  97 invlogit(coef(m.oring)[1])
  98 invlogit(coef(m.oring)[2])
  99 
 100 exp(coef(m.oring)[2])
 101 
 102 Effect("temp",m.oring,
 103        xlevels = list(temp = c(50:60)),
 104        transformation = list(link = logit,inverse = exp))
 105 
 106 
 107 
 108 
 109 require(scales)
 110 demo(plotmath)
 111 
 112 orings$trials <- 6
 113 ggplot(orings,aes(x=temp,y=damage/trials)) +
 114     geom_point() +
 115     geom_smooth(method = "glm", family = "binomial", aes(weight = trials)) +
 116     xlab(expression(paste("temperature (",degree,"F)"))) +
 117     ylab("probability of damage") +
 118     scale_y_continuous(labels = percent)
 119 
 120 ggsave("img/challenger.png")
 121 
 122 ggplot(orings,aes(x=temp,y=damage/trials)) +
 123     geom_point() +
 124     xlim(20,90) +
 125     geom_smooth(method = "glm", family = "binomial",
 126                 aes(weight = trials), fullrange =T) +
 127     xlab(expression(paste("temperature (",degree,"F)"))) +
 128     ylab("probability of damage") +
 129     scale_y_continuous(labels = percent)
 130 
 131 
 132 ggsave("img/challenger2.png")
 133 
 134 
 135 ## Infection Example
 136 
 137 load("infection.rdata")
 138 summary(infection)
 139 
 140 m.inf <- glm(infected~age*sex,family=binomial,
 141                                data=infection)
 142 
 143 summary(m.inf)
 144 
 145 ## coefficients
 146 ### intercepts
 147 invlogit(coef(m.inf)[1])
 148 invlogit(coef(m.inf)[1] + coef(m.inf)[3])
 149 
 150 ### slopes
 151 exp(coef(m.inf)[2])
 152 exp(coef(m.inf)[2] + coef(m.inf)[4])
 153 
 154 exp(30 * coef(m.inf)[2])
 155 exp(30 * (coef(m.inf)[2] + coef(m.inf)[4]))
 156 
 157 solve(0.015657,3.000513)
 158 solve(0.02670685,2.883849)
 159 
 160 ## all effects
 161 
 162 allEffects(m.inf)
 163 
 164 allEffects(m.inf,
 165            xlevels = list(age = seq(0,200,by = 50)))
 166 
 167 
 168 
 169 
 170 
 171 
 172 
 173 
 174 
 175 
 176 
 177 
 178 
 179 
 180 
 181 
 182 
 183 ## comparing different link functions
 184 ggplot(births,aes(x = gestwks, y = as.numeric(lowbw)-1)) +
 185     geom_smooth(method = "glm", family = "binomial",se = T,size = 2) +
 186     geom_smooth(method = "glm", family = binomial(link = "probit"),se = T,size = 2, colour = "red") +
 187     geom_smooth(method = "glm", family = binomial(link = "cloglog"),se = T,size = 2, colour = "green") +
 188     geom_point(shape="|")
 189 

Gespeicherte Dateianhänge

Um Dateianhänge in eine Seite einzufügen sollte unbedingt eine Angabe wie attachment:dateiname benutzt werden, wie sie auch in der folgenden Liste der Dateien erscheint. Es sollte niemals die URL des Verweises ("laden") kopiert werden, da sich diese jederzeit ändern kann und damit der Verweis auf die Datei brechen würde.
  • [laden | anzeigen] (2015-07-19 18:47:12, 6.9 KB) [[attachment:births.rdata]]
  • [laden | anzeigen] (2015-08-16 15:57:15, 5.2 KB) [[attachment:birthsweights.rdata]]
  • [laden | anzeigen] (2015-07-13 13:12:28, 1.1 KB) [[attachment:codebook.txt]]
  • [laden | anzeigen] (2015-10-11 17:34:48, 113.2 KB) [[attachment:dataglmm.zip]]
  • [laden | anzeigen] (2015-09-27 19:34:22, 1.1 KB) [[attachment:datamm.zip]]
  • [laden | anzeigen] (2015-09-21 12:37:51, 0.9 KB) [[attachment:datamod.zip]]
  • [laden | anzeigen] (2015-10-11 17:34:20, 1.9 KB) [[attachment:exercicesmm.r]]
  • [laden | anzeigen] (2015-07-12 16:50:25, 0.5 KB) [[attachment:exercise.r]]
  • [laden | anzeigen] (2015-07-19 18:48:54, 56.8 KB) [[attachment:exercises7mpi.pdf]]
  • [laden | anzeigen] (2015-07-20 12:55:14, 63.9 KB) [[attachment:exercises7rh.pdf]]
  • [laden | anzeigen] (2015-07-26 17:18:23, 95.0 KB) [[attachment:exercises8.pdf]]
  • [laden | anzeigen] (2015-10-12 12:47:51, 60.2 KB) [[attachment:exercisesglmms.pdf]]
  • [laden | anzeigen] (2015-10-11 17:35:38, 1.9 KB) [[attachment:exercisesmm.r]]
  • [laden | anzeigen] (2015-09-20 19:19:09, 62.8 KB) [[attachment:exercisesmod.pdf]]
  • [laden | anzeigen] (2015-10-04 13:16:42, 13.9 KB) [[attachment:gewichte.rdata]]
  • [laden | anzeigen] (2015-08-16 15:57:23, 2.5 KB) [[attachment:infection.rdata]]
  • [laden | anzeigen] (2015-07-12 16:51:18, 23.7 KB) [[attachment:material.zip]]
  • [laden | anzeigen] (2015-08-31 04:21:17, 12.9 KB) [[attachment:material11.zip]]
  • [laden | anzeigen] (2015-09-27 19:34:30, 0.9 KB) [[attachment:mm.r]]
  • [laden | anzeigen] (2015-07-26 17:19:27, 933.0 KB) [[attachment:nhanes1112.xlsx]]
  • [laden | anzeigen] (2015-06-07 16:16:59, 82.3 KB) [[attachment:pract2.pdf]]
  • [laden | anzeigen] (2015-08-16 15:57:03, 3.4 KB) [[attachment:session10exer.r]]
  • [laden | anzeigen] (2015-08-31 09:20:16, 1.7 KB) [[attachment:session11exer.r]]
  • [laden | anzeigen] (2015-06-05 13:09:58, 648.3 KB) [[attachment:session1gettinstarted.pdf]]
  • [laden | anzeigen] (2015-06-05 13:10:08, 178.8 KB) [[attachment:session1readdata.pdf]]
  • [laden | anzeigen] (2015-05-26 12:14:29, 145.4 KB) [[attachment:session1skriptdata.zip]]
  • [laden | anzeigen] (2015-06-07 16:19:51, 277.7 KB) [[attachment:session2.zip]]
  • [laden | anzeigen] (2015-06-14 15:30:31, 1.5 KB) [[attachment:session3.r]]
  • [laden | anzeigen] (2015-06-14 15:32:05, 2759.4 KB) [[attachment:session3.zip]]
  • [laden | anzeigen] (2015-06-21 17:18:54, 2.9 KB) [[attachment:session4.r]]
  • [laden | anzeigen] (2015-06-21 17:17:26, 4.1 KB) [[attachment:session4.zip]]
  • [laden | anzeigen] (2015-07-06 08:22:26, 2.6 KB) [[attachment:session5.r]]
  • [laden | anzeigen] (2015-07-12 16:47:47, 0.9 KB) [[attachment:session6.r]]
  • [laden | anzeigen] (2015-07-19 18:16:41, 0.9 KB) [[attachment:session7mpi.r]]
  • [laden | anzeigen] (2015-07-19 18:16:06, 2.1 KB) [[attachment:session7mpiexer.r]]
  • [laden | anzeigen] (2015-07-19 18:49:43, 1.9 KB) [[attachment:session7rhexerc.r]]
  • [laden | anzeigen] (2015-07-26 17:19:02, 0.8 KB) [[attachment:session8exer.r]]
  • [laden | anzeigen] (2015-08-02 16:54:59, 27.2 KB) [[attachment:session9data.xlsx]]
  • [laden | anzeigen] (2015-08-02 16:54:16, 4.0 KB) [[attachment:session9exer.r]]
  • [laden | anzeigen] (2015-09-27 19:34:47, 312.4 KB) [[attachment:sessionmm.pdf]]
  • [laden | anzeigen] (2015-10-04 13:16:17, 621.8 KB) [[attachment:sessionmm2.pdf]]
  • [laden | anzeigen] (2015-10-04 13:16:57, 0.8 KB) [[attachment:sessionmm2.r]]
  • [laden | anzeigen] (2015-07-09 09:26:13, 1.7 KB) [[attachment:skript2.r]]
  • [laden | anzeigen] (2015-07-09 09:25:51, 1.8 KB) [[attachment:skript3.r]]
  • [laden | anzeigen] (2015-07-09 09:25:33, 4.0 KB) [[attachment:skript4.r]]
  • [laden | anzeigen] (2015-07-09 09:25:21, 4.5 KB) [[attachment:skript5.r]]
  • [laden | anzeigen] (2015-08-16 15:56:40, 1057.3 KB) [[attachment:slides10.pdf]]
  • [laden | anzeigen] (2015-08-31 04:21:52, 697.3 KB) [[attachment:slides11.pdf]]
  • [laden | anzeigen] (2015-06-07 16:16:20, 340.9 KB) [[attachment:slides2.pdf]]
  • [laden | anzeigen] (2015-06-17 11:40:27, 225.9 KB) [[attachment:slides3.pdf]]
  • [laden | anzeigen] (2015-06-21 17:17:54, 651.2 KB) [[attachment:slides4.pdf]]
  • [laden | anzeigen] (2015-07-06 08:18:56, 587.8 KB) [[attachment:slides5.pdf]]
  • [laden | anzeigen] (2015-07-12 16:45:07, 256.4 KB) [[attachment:slides6.pdf]]
  • [laden | anzeigen] (2015-07-12 16:45:29, 266.2 KB) [[attachment:slides6a.pdf]]
  • [laden | anzeigen] (2015-07-19 15:47:33, 129.6 KB) [[attachment:slides7mpi.pdf]]
  • [laden | anzeigen] (2015-07-19 15:48:30, 740.6 KB) [[attachment:slides7rh.pdf]]
  • [laden | anzeigen] (2015-07-26 17:18:44, 545.8 KB) [[attachment:slides8.pdf]]
  • [laden | anzeigen] (2015-08-02 16:54:35, 573.2 KB) [[attachment:slides9.pdf]]
  • [laden | anzeigen] (2015-08-23 11:27:40, 121.1 KB) [[attachment:slidesMD.pdf]]
  • [laden | anzeigen] (2015-06-14 15:28:09, 1.7 KB) [[attachment:solutions.r]]
  • [laden | anzeigen] (2015-06-05 13:10:43, 1666.4 KB) [[attachment:userinterfaces.pdf]]
  • [laden | anzeigen] (2015-08-23 11:26:22, 212.7 KB) [[attachment:woodboring.pdf]]
  • [laden | anzeigen] (2015-06-07 16:17:19, 190.7 KB) [[attachment:wrapup.pdf]]
 Alle Dateien | Ausgewählte Dateien: löschen verschieben auf Seite copy to page

Sie dürfen keine Anhänge an diese Seite anhängen!