@@ -16,9 +16,9 @@ library(kableExtra)
16
16
17
17
# Introduction
18
18
19
-
20
- ![ Artwork by @allison_horst: "love this class"] ( images/love-this-class.png ) {fig-alt="A little monster flying a biplane wearing aviator glasses, pulling a banner that says 'Fully expecting to hate this class.' Below, a teacher wearing a cheerleading outfit labeled 'STATS' with a bullhorn labeled 'CODE' cheering desperately with pom-poms, trying to help students believe stats is actually going to be awesomely life-changing." width="800"}
21
-
19
+ ![ Artwork by @allison_horst: "love this
20
+ class"] ( images/love-this-class.png ) {fig-alt="A little monster flying a biplane wearing aviator glasses, pulling a banner that says 'Fully expecting to hate this class.' Below, a teacher wearing a cheerleading outfit labeled 'STATS' with a bullhorn labeled 'CODE' cheering desperately with pom-poms, trying to help students believe stats is actually going to be awesomely life-changing."
21
+ width="800"}
22
22
23
23
## Session overview
24
24
114
114
115
115
$\bar{x} \pm 1.96 \times s.e.$
116
116
117
- Where 1.96 is the quantile for 95% confidence.
117
+ This means the upper limit is $\bar{x} + 1.96 \times s.e.$ and the lower
118
+ limit is $\bar{x} - 1.96 \times s.e.$. 1.96 is the "quantile" for 95%
119
+ confidence and can be found using the ` qnorm() ` function.
118
120
119
121
![ ] ( images/do_on_your_computer.png ) Save
120
122
[ beewing.txt] ( data-raw/beewing.txt ) to your ` data-raw ` folder.
@@ -131,58 +133,69 @@ beewing <- read_table("data-raw/beewing.txt")
131
133
str(beewing)
132
134
```
133
135
134
- ![ ] ( images/do_in_R.png ) Calculate and assign to variables: the mean,
135
- standard deviation, sample size and standard error:
136
+ ![ ] ( images/do_in_R.png ) Use the ` summarise() ` function to calculate: the
137
+ mean, standard deviation, sample size and standard error and save them
138
+ in a dataframe called ` beewing_summary ` :
139
+
136
140
137
141
``` {r}
138
- # mean
139
- m <- mean(beewing$wing_mm)
142
+ beewing_summary <- beewing |>
143
+ summarise(mean = mean(wing_mm),
144
+ n = length(wing_mm),
145
+ sd = sd(wing_mm),
146
+ se = sd / sqrt(n))
147
+ ```
140
148
141
- # standard deviation
142
- sd <- sd(beewing$wing_mm)
149
+ To see the values in the ` beewing_summary ` dataframe you can either
150
+ type and run ` beewing_summary ` or click on the dataframe in the
151
+ Environment tab.
143
152
144
- # sample size (needed for the se)
145
- n <- length(beewing$wing_mm)
146
153
147
- # standard error
148
- se <- sd / sqrt(n)
149
- ```
154
+ ::: callout-note
155
+ ## Where did you do this before?
150
156
151
- ![ ] ( images/do_in_R.png ) To calculate the 95% confidence interval we need
152
- to look up the quantile (multiplier) using ` qnorm() ` :
157
+ In the BABS 1 week 8 workshop we [ summarised the mass of cats ] ( ../../r4babs1/week-8/workshop.html#cat-mass ) using this
158
+ method. You can look back at that if you need to.
153
159
154
- ``` {r}
155
- q <- qnorm(0.975)
156
- ```
160
+ :::
157
161
158
- This should be about 1.96.
159
162
160
- ![ ] ( images/do_in_R.png ) Now we can use it in our confidence interval
161
- calculation :
163
+ To calculate the 95% confidence interval we need to look up the
164
+ quantile (multiplier) using ` qnorm() ` :
162
165
163
166
``` {r}
164
- lcl <- m - q * se
165
- ucl <- m + q * se
167
+ qnorm(0.975)
166
168
```
167
169
168
- I used the names ` lcl ` and ` ucl ` to stand for "lower confidence limit"
169
- and "upper confidence limit" respectively.
170
+ This should be about 1.96. We can use this piece of code along with
171
+ the ` mean ` and ` se ` columns to add the upper and lower confidence
172
+ limits to the ` beewing_summary ` dataframe using ` mutate() ` :
173
+
170
174
171
- ![ ] ( images/do_in_R.png ) Print the values:
175
+ ![ ] ( images/do_in_R.png ) Add the upper and lower confidence
176
+ limits to the ` beewing_summary ` dataframe:
172
177
173
178
``` {r}
174
- lcl
175
- ucl
179
+ beewing_summary <- beewing_summary |>
180
+ mutate(lcl95 = mean - qnorm(0.975) * se,
181
+ ucl95 = mean + qnorm(0.975) * se)
176
182
```
177
183
184
+
185
+ I used the names ` lcl95 ` and ` ucl95 ` to stand for "95% lower confidence
186
+ limit" and "95% upper confidence limit" respectively.
187
+
188
+
178
189
This means we are 95% confident the population mean lies between
179
- ` r round(lcl,2) ` mm and ` r round(ucl,2) ` mm.
190
+ ` r round(beewing_summary$lcl95,2) ` mm and
191
+ ` r round(beewing_summary$ucl95,2) ` mm.
180
192
181
193
![ ] ( images/do_in_R.png ) How would you write this up in a report?
182
194
183
195
<!-- #---THINKING ANSWER--- -->
184
196
185
197
<!-- The left wing of bees have a mean width of 4.55 mm, -->
198
+
186
199
<!-- 95% C.I. [4.47, 4.63]. -->
187
200
188
201
![ ] ( images/do_in_R.png ) Between what values would you be * 99%* confident
@@ -194,9 +207,9 @@ of the population mean being?
194
207
#---CODING ANSWER---
195
208
196
209
# qnorm(0.975) gives the quantile for 95%. For 99% we need qnorm(0.995)
197
- q <- qnorm(0.995)
198
- lcl <- m - q * se
199
- ucl <- m + q * se
210
+ beewing_summary <- beewing_summary |>
211
+ mutate(lcl99 = mean - qnorm(0.975) * se,
212
+ ucl99 = mean + qnorm(0.975) * se)
200
213
201
214
```
202
215
@@ -247,69 +260,43 @@ resulting dataframe
247
260
#| include: false
248
261
249
262
#---CODING ANSWER---
250
- neur <- read_table("data-raw/neuron.txt")
263
+ neuron <- read_table("data-raw/neuron.txt")
251
264
```
252
265
253
- ![ ] ( images/do_in_R.png ) Assign the mean to ` m ` .
266
+ ![ ] ( images/do_in_R.png ) Calculate: the mean, standard deviation,
267
+ sample size and standard error and save them in a dataframe called
268
+ ` neuron_summary ` :
254
269
255
270
``` {r}
256
- #| include: false
257
-
258
- #---CODING ANSWER---
259
-
260
- m <- mean(neur$csa)
261
-
262
- ```
263
-
264
- ![ ] ( images/do_in_R.png ) Calculate and assign the standard error to ` se ` .
265
-
266
- ``` {r}
267
- #| include: false
268
-
269
- #---CODING ANSWER---
270
-
271
- # I created intermediate variables for sd and n but you may have done
272
- # in a single line
273
- sd <- sd(neur$csa)
274
- n <- length(neur$csa)
275
- se <- sd / sqrt(n)
271
+ neuron_summary <- neuron |>
272
+ summarise(mean = mean(csa),
273
+ n = length(csa),
274
+ sd = sd(csa),
275
+ se = sd / sqrt(n))
276
276
```
277
277
278
278
To work out the confidence interval for our sample mean we need to use
279
279
the * t* distribution because it is a small sample. This means we need to
280
280
determine the degrees of freedom (the number in the sample minus one).
281
281
282
- ![ ] ( images/do_in_R.png ) We can assign this to a variable, ` df ` , using:
283
-
284
- ``` {r}
285
- df <- length(neur$csa) - 1
286
- ```
287
-
288
- ![ ] ( images/do_in_R.png ) The * t* value is found by:
282
+ The * t* value is found using: ` qt(0.975, df = n - 1) ` . Note that we are
283
+ using ` qt() ` rather than ` qnorm() ` but that the probability, 0.975,
284
+ used is the same.
289
285
290
- ``` {r}
291
- t <- qt(0.975, df = df)
292
- ```
286
+ This should be about 2.36. This is bigger than 1.96 to reflect the lower
287
+ confidence we have in a mean from a small sample. We can use this piece
288
+ of code along with the ` mean ` and ` se ` columns to add the upper and
289
+ lower confidence
290
+ limits to the ` neuron_summary ` dataframe using ` mutate() ` :
293
291
294
- Note that we are using ` qt() ` rather than ` qnorm() ` but that the
295
- probability, 0.975, used is the same. Finally, we need to put our mean,
296
- standard error and * t* value in the equation.
297
- $\bar{x} \pm t_ {[ d.f] } \times s.e.$.
298
292
299
- ![ ] ( images/do_in_R.png ) The upper confidence limit is:
293
+ ![ ] ( images/do_in_R.png ) Add the upper and lower confidence
294
+ limits to the ` neuron_summary ` dataframe:
300
295
301
296
``` {r}
302
- (m + t * se) |> round(2)
303
- ```
304
-
305
- The first part of the command, ` (m + t * se) ` calculates the upper
306
- limit. This is 'piped' in to the ` round() ` function to round the result
307
- to two decimal places.
308
-
309
- ![ ] ( images/do_in_R.png ) Calculate the lower confidence limit:
310
-
311
- ``` {r include=FALSE}
312
- (m - t * se) |> round(2)
297
+ neuron_summary <- neuron_summary |>
298
+ mutate(lcl95 = mean - qt(0.975, df = n - 1) * se,
299
+ ucl95 = mean + qt(0.975, df = n - 1) * se)
313
300
```
314
301
315
302
![ ] ( images/answer.png ) Given the upper and lower confidence values for
@@ -349,18 +336,17 @@ then "Compressed (zipped) folder". This will create a file called
349
336
` week-1.zip ` . Email this file to someone near you have have them email
350
337
you with theirs. Your neighbour should be able to download ` week-1.zip ` ,
351
338
unzip it and then open the project in RStudio and run the code to
352
- reproduce all your work. * * Note: Save the downloaded ` week-1.zip ` some
353
- where that is NOT your "data-analysis-in-r-2" to avoid naming
354
- conflicts.* * Also do not save it in any RStudio project folder.
339
+ reproduce all your work. \*\ * Note: Save the downloaded ` week-1.zip `
340
+ some where that is NOT your "data-analysis-in-r-2" to avoid naming
341
+ conflicts.\*\ * Also do not save it in any RStudio project folder.
355
342
356
343
You're finished!
357
344
358
345
# 🥳 Well Done! 🎉
359
346
360
347
![ Artwork by @allison_horst: "We belive in
361
- you!"] ( images/we-believe.png ) {fig-alt="Header text 'R learners' above
362
- five friendly monsters holding up signs that together read 'we believe
363
- in you.'"width="800"}
348
+ you!"] ( images/we-believe.png ) {fig-alt="Header text 'R learners' above five friendly monsters holding up signs that together read 'we believe in you.'"
349
+ width="800"}
364
350
365
351
# Independent study following the workshop
366
352
@@ -379,8 +365,7 @@ Browser](https://github.com/3mmaRand/R4BABS/blob/main/r4babs2/week-1/workshop.qm
379
365
Coding and thinking answers are marked with ` #---CODING ANSWER--- ` and
380
366
` #---THINKING ANSWER--- `
381
367
382
- Pages made with R [ @R-core ] , Quarto [ @allaire2022 ] , ` knitr `
383
- [ @knitr1 ; @knitr2 ; @knitr3 ] ,
384
- ` kableExtra ` [ @kableExtra ]
368
+ Pages made with R [ @R-core ] , Quarto [ @allaire2022 ] , ` knitr ` [ @knitr1 ;
369
+ @knitr2 ; @knitr3 ] , ` kableExtra ` [ @kableExtra ]
385
370
386
371
# References
0 commit comments