Skip to content

Commit 2c025d3

Browse files
authored
Merge pull request #46 from 3mmaRand/fixx/babs2-confidence-int
rewrite ci to use summarise and not $ in workshop and consolidate
2 parents 07ecd04 + 86b6f47 commit 2c025d3

File tree

2 files changed

+78
-94
lines changed

2 files changed

+78
-94
lines changed

r4babs2/week-1/study_after_workshop.qmd

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ adip_summary <- adip %>%
4141
sd = sd(adiponectin),
4242
n = length(adiponectin),
4343
se = sd/sqrt(n),
44-
dif = qt(0.975, df = n - 1) * se,
45-
lower_ci = mean - dif,
46-
uppp_ci = mean + dif)
44+
lcl95 = mean - qt(0.975, df = n - 1) * se,
45+
ucl95 = mean + qt(0.975, df = n - 1) * se)
4746
4847
4948
# we conclude we're 95% certain the mean for the control group is

r4babs2/week-1/workshop.qmd

Lines changed: 76 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ library(kableExtra)
1616

1717
# Introduction
1818

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"}
2222

2323
## Session overview
2424

@@ -114,7 +114,9 @@ by:
114114

115115
$\bar{x} \pm 1.96 \times s.e.$
116116

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.
118120

119121
![](images/do_on_your_computer.png) Save
120122
[beewing.txt](data-raw/beewing.txt) to your `data-raw` folder.
@@ -131,58 +133,69 @@ beewing <- read_table("data-raw/beewing.txt")
131133
str(beewing)
132134
```
133135

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+
136140

137141
```{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+
```
140148

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.
143152

144-
# sample size (needed for the se)
145-
n <- length(beewing$wing_mm)
146153

147-
# standard error
148-
se <- sd / sqrt(n)
149-
```
154+
::: callout-note
155+
## Where did you do this before?
150156

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.
153159

154-
```{r}
155-
q <- qnorm(0.975)
156-
```
160+
:::
157161

158-
This should be about 1.96.
159162

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()`:
162165

163166
```{r}
164-
lcl <- m - q * se
165-
ucl <- m + q * se
167+
qnorm(0.975)
166168
```
167169

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+
170174

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:
172177

173178
```{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)
176182
```
177183

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+
178189
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.
180192

181193
![](images/do_in_R.png) How would you write this up in a report?
182194

183195
<!-- #---THINKING ANSWER--- -->
184196

185197
<!-- The left wing of bees have a mean width of 4.55 mm, -->
198+
186199
<!-- 95% C.I. [4.47, 4.63]. -->
187200

188201
![](images/do_in_R.png) Between what values would you be *99%* confident
@@ -194,9 +207,9 @@ of the population mean being?
194207
#---CODING ANSWER---
195208
196209
# 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)
200213
201214
```
202215

@@ -247,69 +260,43 @@ resulting dataframe
247260
#| include: false
248261
249262
#---CODING ANSWER---
250-
neur <- read_table("data-raw/neuron.txt")
263+
neuron <- read_table("data-raw/neuron.txt")
251264
```
252265

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`:
254269

255270
```{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))
276276
```
277277

278278
To work out the confidence interval for our sample mean we need to use
279279
the *t* distribution because it is a small sample. This means we need to
280280
determine the degrees of freedom (the number in the sample minus one).
281281

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.
289285

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()`:
293291

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.$.
298292

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:
300295

301296
```{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)
313300
```
314301

315302
![](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
349336
`week-1.zip`. Email this file to someone near you have have them email
350337
you with theirs. Your neighbour should be able to download `week-1.zip`,
351338
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.
355342

356343
You're finished!
357344

358345
# 🥳 Well Done! 🎉
359346

360347
![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"}
364350

365351
# Independent study following the workshop
366352

@@ -379,8 +365,7 @@ Browser](https://github.com/3mmaRand/R4BABS/blob/main/r4babs2/week-1/workshop.qm
379365
Coding and thinking answers are marked with `#---CODING ANSWER---` and
380366
`#---THINKING ANSWER---`
381367

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]
385370

386371
# References

0 commit comments

Comments
 (0)