Skip to content

Commit

Permalink
168
Browse files Browse the repository at this point in the history
  • Loading branch information
Emily Nordmann committed Oct 17, 2023
1 parent 6a10c10 commit 9674648
Show file tree
Hide file tree
Showing 43 changed files with 2,144 additions and 870 deletions.
48 changes: 6 additions & 42 deletions book/06-corsi-analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,11 @@

```{r echo = FALSE, message=FALSE, warning=FALSE}
library(tidyverse)
# Set the seed for reproducibility
set.seed(123)
# Define the number of participants
n_participants <- 600
# Define the variables
Gender <- c(rep('Woman', n_participants * 0.5),
rep('Man', n_participants * 0.40),
rep('Non-Binary', n_participants * 0.1))
Age <- sample(18:60, n_participants, replace = TRUE)
# Set conditions and mean Corsi scores
Condition <- c(rep('8 hours sleep', n_participants * 1/3),
rep('4 hours sleep', n_participants * 1/3),
rep('Sleep deprived', n_participants * 1/3))
Mean_Corsi_Score <- ifelse(Condition == '8 hours sleep', 6, ifelse(Condition == '4 hours sleep', 5, 4))
# Simulate Corsi scores around mean, considering a small correlation with age
Corsi_Score <- round(rnorm(n_participants, Mean_Corsi_Score + 0.01*(Age - mean(Age)), sd = 1))
Corsi_Score <- ifelse(Corsi_Score > 9, 9, ifelse(Corsi_Score < 0, 0, Corsi_Score))
# Create a data frame
data <- data.frame(Participant = 1:n_participants,
Gender = sample(Gender),
Age = Age,
Condition = Condition,
Corsi_Score = Corsi_Score)
# Define Demographic data
demographic_data <- data %>%
select(Participant, Gender, Age, Condition)
# Define Score data
score_data <- data %>%
select(Participant, Corsi_Score)
full_dat <- inner_join(x = demographic_data, y = score_data, by = "Participant")
demographic_data <- read_csv("data/corsi/demographic_data.csv")
score_data <- read_csv("data/corsi/score_data.csv")
full_dat <- inner_join(x = demographic_data,
y = score_data,
by = "Participant")
```

Expand Down Expand Up @@ -215,7 +179,7 @@ age_control <- analysis_data %>%
`r unhide()`


How many participants are left in the sample after filtering for age? `r fitb(163)`
How many participants are left in the sample after filtering for age? `r fitb(168)`

## Activity 5: Summarise

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 74 additions & 3 deletions book/packages.bib
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ @Manual{R-dplyr
url = {https://CRAN.R-project.org/package=dplyr},
}

@Manual{R-faux,
title = {faux: Simulation for Factorial Designs},
author = {Lisa DeBruine},
year = {2023},
note = {R package version 1.2.1},
url = {https://github.com/debruine/faux},
}

@Manual{R-forcats,
title = {forcats: Tools for Working with Categorical Variables (Factors)},
author = {Hadley Wickham},
Expand All @@ -40,11 +48,26 @@ @Manual{R-ggplot2
}

@Manual{R-glossary,
title = {glossary: Glossaries for Markdown and Quarto Documents},
title = {glossary: PsyTeachR Glossary of Statistical and Coding Terms},
author = {Lisa DeBruine},
year = {2022},
note = {R package version 0.0.0.9002},
}

@Manual{R-kableExtra,
title = {kableExtra: Construct Complex Table with kable and Pipe Syntax},
author = {Hao Zhu},
year = {2021},
note = {R package version 1.3.4},
url = {https://CRAN.R-project.org/package=kableExtra},
}

@Manual{R-knitr,
title = {knitr: A General-Purpose Package for Dynamic Report Generation in R},
author = {Yihui Xie},
year = {2023},
note = {R package version 1.0.0},
url = {https://CRAN.R-project.org/package=glossary},
note = {R package version 1.42},
url = {https://yihui.org/knitr/},
}

@Manual{R-lubridate,
Expand All @@ -71,6 +94,14 @@ @Manual{R-readr
url = {https://CRAN.R-project.org/package=readr},
}

@Manual{R-rvest,
title = {rvest: Easily Harvest (Scrape) Web Pages},
author = {Hadley Wickham},
year = {2022},
note = {R package version 1.0.3},
url = {https://CRAN.R-project.org/package=rvest},
}

@Manual{R-stringr,
title = {stringr: Simple, Consistent Wrappers for Common String Operations},
author = {Hadley Wickham},
Expand Down Expand Up @@ -121,6 +152,14 @@ @Manual{R-webexercises
url = {https://github.com/psyteachr/webexercises},
}

@Manual{R-xml2,
title = {xml2: Parse XML},
author = {Hadley Wickham and Jim Hester and Jeroen Ooms},
year = {2023},
note = {R package version 1.3.4},
url = {https://CRAN.R-project.org/package=xml2},
}

@Book{bookdown2016,
title = {bookdown: Authoring Books and Technical Documents with {R} Markdown},
author = {Yihui Xie},
Expand All @@ -140,6 +179,38 @@ @Book{ggplot22016
url = {https://ggplot2.tidyverse.org},
}

@Manual{glossary2022,
title = {PsyTeachR Glossary of Statistical and Coding Terms},
author = {{PsyTeachR Team}},
year = {2022},
url = {https://psyteachr.github.io/glossary/},
doi = {10.5281/zenodo.6619215},
version = {0.0.0.9002},
publisher = {Zenodo},
note = {v0.0.0.9002},
}

@Book{knitr2015,
title = {Dynamic Documents with {R} and knitr},
author = {Yihui Xie},
publisher = {Chapman and Hall/CRC},
address = {Boca Raton, Florida},
year = {2015},
edition = {2nd},
note = {ISBN 978-1498716963},
url = {https://yihui.org/knitr/},
}

@InCollection{knitr2014,
booktitle = {Implementing Reproducible Computational Research},
editor = {Victoria Stodden and Friedrich Leisch and Roger D. Peng},
title = {knitr: A Comprehensive Tool for Reproducible Research in {R}},
author = {Yihui Xie},
publisher = {Chapman and Hall/CRC},
year = {2014},
note = {ISBN 978-1466561595},
}

@Article{lubridate2011,
title = {Dates and Times Made Easy with {lubridate}},
author = {Garrett Grolemund and Hadley Wickham},
Expand Down
Binary file modified book/template.rds
Binary file not shown.
32 changes: 16 additions & 16 deletions docs/01-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ There is a walkthrough video of this chapter available via [Echo360.](https://ec

## R and RStudio {#sec-intro-r}

R is a programming language that you will write code in. RStudio is an Integrated Development Environment (<a class='glossary'>IDE<span class='def'></span></a>). Think of it as knowing English and using a plain text editor like NotePad to write a book versus using a word processor like Microsoft Word. You could do it, but it would be much harder without things like spell-checking and formatting and you wouldn't be able to use some of the advanced features that Word has developed. In a similar way, you can use R without R Studio but we wouldn't recommend it. RStudio serves as a text editor, file manager, spreadsheet viewer, and more.
R is a programming language that you will write code in. RStudio is an Integrated Development Environment (<a class='glossary' target='_blank' title='Integrated Development Environment: a program that serves as a text editor, file manager, and provides functions to help you read and write code. RStudio is an IDE for R.' href='https://psyteachr.github.io/glossary/i#ide'>IDE</a>). Think of it as knowing English and using a plain text editor like NotePad to write a book versus using a word processor like Microsoft Word. You could do it, but it would be much harder without things like spell-checking and formatting and you wouldn't be able to use some of the advanced features that Word has developed. In a similar way, you can use R without R Studio but we wouldn't recommend it. RStudio serves as a text editor, file manager, spreadsheet viewer, and more.

* There's an [RStudio IDE Cheatsheet](https://raw.githubusercontent.com/rstudio/cheatsheets/main/rstudio-ide.pdf){download=""} you will find helpful to download and keep as a reference.

Expand Down Expand Up @@ -72,7 +72,7 @@ Do the following, in this order:

## RStudio panes

RStudio is arranged with four window <a class='glossary'>panes<span class='def'></span></a>. By default, the upper left pane is the **source pane**, where you view, write, and edit code from files and view data tables in a spreadsheet format. **When you first open a new project this pane won't display until you open a document or load in some data** -- don't worry, we'll get to that soon.
RStudio is arranged with four window <a class='glossary' target='_blank' title='RStudio is arranged with four window "panes".' href='https://psyteachr.github.io/glossary/p#panes'>panes</a>. By default, the upper left pane is the **source pane**, where you view, write, and edit code from files and view data tables in a spreadsheet format. **When you first open a new project this pane won't display until you open a document or load in some data** -- don't worry, we'll get to that soon.

<div class="figure" style="text-align: center">
<img src="images/intro/rstudio.png" alt="The RStudio IDE" width="100%" />
Expand All @@ -85,7 +85,7 @@ The lower left pane is the **console pane**, where you can type in commands and
Try using the console as a calculator - type `1 + 1` and press enter.
:::

The right panes have several different tabs that show you information about your code. The most used tab in the upper right pane is the **Environment** tab. The Environment tab lists some information about the <a class='glossary'>objects<span class='def'></span></a> that you have defined in your code.
The right panes have several different tabs that show you information about your code. The most used tab in the upper right pane is the **Environment** tab. The Environment tab lists some information about the <a class='glossary' target='_blank' title='A word that identifies and stores the value of some data for later use.' href='https://psyteachr.github.io/glossary/o#object'>objects</a> that you have defined in your code.

In the lower right pane, the most used tabs are the **Files** tab for directory structure, the **Plots** tab for plots, the **Packages** tab for managing add-on packages (we'll explain what these are in the next chapter), and the **Viewer** tab to display reports created by your scripts. You can change the location of panes and what tabs are shown under `Tools > Global Options... > Pane Layout`. When you open a new project, the Files tab will be set to your project working directory and will show a file with the file extension `.Rproj` which is the file that has all the information about your project. Don't delete this file!

Expand Down Expand Up @@ -141,8 +141,8 @@ birthday <- as.Date("2024-07-11")

You'll see that four objects now appear in the environment pane:

* `name` is <a class='glossary'>character<span class='def'></span></a> (text) data. In order for R to recognise it as text, it **must** be enclosed in double quotation marks `" "`.
* `age` is <a class='glossary'>numeric<span class='def'></span></a> data. In order for R to recognise this as a number, it **must not** be enclosed in quotation marks. Every year I have to update my age in this book and I am confronted with my own mortality drawing ever closer.
* `name` is <a class='glossary' target='_blank' title='A data type representing strings of text.' href='https://psyteachr.github.io/glossary/c#character'>character</a> (text) data. In order for R to recognise it as text, it **must** be enclosed in double quotation marks `" "`.
* `age` is <a class='glossary' target='_blank' title='A data type representing a real decimal number or integer.' href='https://psyteachr.github.io/glossary/n#numeric'>numeric</a> data. In order for R to recognise this as a number, it **must not** be enclosed in quotation marks. Every year I have to update my age in this book and I am confronted with my own mortality drawing ever closer.
* `today` stores the result of the function `Sys.Date()`. This function returns your computer system's date. Unlike `name` and `age`, which are hard-coded (i.e., they will always return the values you enter), the contents of the object `today` will change dynamically with the date. That is, if you run that function tomorrow, it will update the date to tomorrow's date.
* `birthday` is also a date but it's hard-coded as a specific date. It's wrapped within the `as.Date()` function that tells R to interpret the character string you provide as a date rather than text.

Expand Down Expand Up @@ -258,7 +258,7 @@ My name is `r name` and I am `r age` years old. It is `r birthday - today` days

As if by magic, that slightly odd bit of text you copied and pasted now appears as a normal sentence with the values pulled in from the objects you created.

**My name is Emily and I am 38 years old. It is 271 days until my birthday.**
**My name is Emily and I am 38 years old. It is 268 days until my birthday.**

We're not going to use this function very often in the rest of the course but hopefully you can see just how useful this would be when writing up a report with lots of numbers. R Markdown is an incredibly powerful and flexible format - this book was written using it!

Expand Down Expand Up @@ -311,24 +311,24 @@ The glossary at the end of each chapter defines common jargon you might encounte
</thead>
<tbody>
<tr>
<td style="text-align:left;"> character </td>
<td style="text-align:left;"> </td>
<td style="text-align:left;"> [character](https://psyteachr.github.io/glossary/c.html#character){class="glossary" target="_blank"} </td>
<td style="text-align:left;"> A data type representing strings of text. </td>
</tr>
<tr>
<td style="text-align:left;"> IDE </td>
<td style="text-align:left;"> </td>
<td style="text-align:left;"> [ide](https://psyteachr.github.io/glossary/i.html#ide){class="glossary" target="_blank"} </td>
<td style="text-align:left;"> Integrated Development Environment: a program that serves as a text editor, file manager, and provides functions to help you read and write code. RStudio is an IDE for R. </td>
</tr>
<tr>
<td style="text-align:left;"> numeric </td>
<td style="text-align:left;"> </td>
<td style="text-align:left;"> [numeric](https://psyteachr.github.io/glossary/n.html#numeric){class="glossary" target="_blank"} </td>
<td style="text-align:left;"> A data type representing a real decimal number or integer. </td>
</tr>
<tr>
<td style="text-align:left;"> object </td>
<td style="text-align:left;"> </td>
<td style="text-align:left;"> [object](https://psyteachr.github.io/glossary/o.html#object){class="glossary" target="_blank"} </td>
<td style="text-align:left;"> A word that identifies and stores the value of some data for later use. </td>
</tr>
<tr>
<td style="text-align:left;"> panes </td>
<td style="text-align:left;"> </td>
<td style="text-align:left;"> [panes](https://psyteachr.github.io/glossary/p.html#panes){class="glossary" target="_blank"} </td>
<td style="text-align:left;"> RStudio is arranged with four window "panes". </td>
</tr>
</tbody>
</table>
Expand Down
Loading

0 comments on commit 9674648

Please sign in to comment.