Dateianhang 'skript4.r'
Herunterladen 1 ## load the data
2 load("data/studrecords.rdata")
3
4 ## Test the null hypothesis that the population mean math score
5 ## is 500 against a two-sided alternative. Would you accept or
6 ## reject at a 0.05 significance level?
7
8 t.test(stud.recs$sat.m, mu = 500)
9
10 ## Test the null hypothesis that the mean math score is equal
11 ## to the mean verbal (sat.v) score against a two sided hypothesis.
12 t.test(stud.recs$sat.m, stud.recs$sat.v)
13
14
15 ## Test the null hypothesis that the verbal and the math score
16 ## is equal in each student.
17 t.test(stud.recs$sat.m, stud.recs$sat.v, paired = T)
18
19
20 ## Wilcoxon signed-rank test
21 pre.test <- c(17,12,20,12,20,21,23,10,15,17,18,18)
22 post.test <- c(19,25,18,18,26,19,27,14,20,22,16,18)
23
24 wilcox.test(pre.test,post.test,paired = T)
25
26 ## exercises
27 ## load the data frame normtemp from the file temperature.rdata;
28 ## it contains the body temperature of several individuals,
29 ## the gender and the heart rate
30 load("data/temperature.rdata")
31
32
33 ## test if the temperature is different in male (coded as 1)
34 ## and female (coded as 2), use the appropriate test.
35 t.test(temperature ~ gender, data = normtemp)
36
37
38 ## test again, compare the results of the t test and the wilcoxon.
39 wilcox.test(temperature ~ gender, data = normtemp)
40
41
42 require(ggplot2)
43 ggplot(normtemp, aes(x = factor(gender), y = temperature)) +
44 geom_boxplot()
45
46
47
48 ## add another layer
49 ggplot(normtemp, aes(x = factor(gender), y = temperature)) +
50 geom_boxplot() +
51 geom_point()
52
53 ggsave("boxplotpoint.png")
54
55
56 ggplot(normtemp, aes(x = factor(gender), y = temperature)) +
57 geom_boxplot() +
58 geom_jitter()
59
60
61 ggsave("boxplotjitter.png")
62
63
64
65 ggplot(normtemp, aes(x = factor(gender), y = temperature)) +
66 geom_boxplot() +
67 geom_jitter(position=position_jitter(width=0.1))
68
69
70 ggsave("boxplotjitter2.png")
71
72 ggplot(normtemp, aes(x = factor(gender), y = temperature)) +
73 geom_boxplot() +
74 geom_point(stat = "summary", fun.y = "mean")
75
76 ggsave("boxplotmean.png")
77
78
79 ggplot(normtemp, aes(x = factor(gender), y = temperature)) +
80 geom_boxplot() +
81 geom_point(stat = "summary", fun.y = "mean",
82 size = 5, colour = "red")
83
84 ggsave("boxplotmean2.png")
85
86
87
88
89 ## Exercises
90
91 ## load the data set GaltonFamilies from the package HistData
92 require(HistData)
93
94 ## make a boxplot of childHeight dependent on gender
95 ## add the means using geom_point()
96 ggplot(GaltonFamilies, aes(x = gender, y = childHeight)) +
97 geom_boxplot() +
98 geom_point(stat = "summary", fun.y = "mean",
99 size = 5, colour = "red") +
100 geom_text(aes(label = round(..y..,1)), stat = "summary", fun.y = "mean",
101 size = 5, colour = "red",vjust=-0.6)
102
103
104 ## do the respective t-test: what is the (trivial) null hypothesis,
105 ## what is appropriate test, what is the test result and what is
106 ## the conclusion?
107 t.test(childHeight ~ gender, data = GaltonFamilies)
108
109
110 ## bivariate plots
111 ggplot(GaltonFamilies, aes(x=mother,y=father)) +
112 geom_point()
113
114
115 ggsave("scatter.png")
116
117 ## adding a trend line
118 ggplot(GaltonFamilies, aes(x=mother,y=father)) +
119 geom_point() +
120 geom_smooth()
121
122 ggsave("scattertrend1.png")
123
124
125
126 ## adding a trend line
127 ggplot(GaltonFamilies, aes(x=mother,y=father)) +
128 geom_point() +
129 geom_smooth(method="lm")
130
131 ggsave("scattertrend2.png")
132
133
134 ## Exercises
135 ## use again the GaltonFamilies data set; produce a scatter plot
136 ## of childHeight vs midparentHeight
137
138 ggplot(GaltonFamilies, aes(x=midparentHeight,y=childHeight)) +
139 geom_point()
140
141
142 ##add a trend line by using geom\_smooth() without any
143 ## arguments. Which method is used?
144 ggplot(GaltonFamilies, aes(x=midparentHeight,y=childHeight)) +
145 geom_point() +
146 geom_smooth()
147
148
149 ## add a second trend line, this time a linear one!
150 ggplot(GaltonFamilies, aes(x=midparentHeight,y=childHeight)) +
151 geom_point() +
152 geom_smooth() +
153 geom_smooth(method="lm")
154
155
156 ## now use the aesthetic colour in the first line of the plot
157 ## definition. What happens?
158 ggplot(GaltonFamilies, aes(x=midparentHeight,y=childHeight,colour=gender)) +
159 geom_point() +
160 geom_smooth() +
161 geom_smooth(method="lm")
162
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.Sie dürfen keine Anhänge an diese Seite anhängen!