Exercises

Data

bfi data

Load the {psych} package and use the bfi data:

library(psych)
head(bfi)
      A1 A2 A3 A4 A5 C1 C2 C3 C4 C5 E1 E2 E3 E4 E5 N1 N2 N3 N4 N5 O1 O2 O3 O4
61617  2  4  3  4  4  2  3  3  4  4  3  3  3  4  4  3  4  2  2  3  3  6  3  4
61618  2  4  5  2  5  5  4  4  3  4  1  1  6  4  3  3  3  3  5  5  4  2  4  3
61620  5  4  5  4  4  4  5  4  2  5  2  4  4  4  5  4  5  4  2  3  4  2  5  5
61621  4  4  6  5  5  4  4  3  5  5  5  3  4  4  4  2  5  2  4  1  3  3  4  3
61622  2  3  3  4  5  4  4  5  3  2  2  2  5  4  5  2  3  4  4  3  3  3  4  3
61623  6  6  5  6  5  6  6  6  1  3  2  1  6  5  6  3  5  2  2  3  4  3  5  6
      O5 gender education age
61617  3      1        NA  16
61618  3      2        NA  18
61620  2      2        NA  17
61621  5      2        NA  17
61622  3      1        NA  17
61623  1      2         3  21

From the data description (see ?psych::bfi):

25 personality self report items taken from the International Personality Item Pool (ipip.ori.org) were included as part of the Synthetic Aperture Personality Assessment (SAPA) web based personality assessment project. The data from 2800 subjects are included here as a demonstration set for scale construction, factor analysis, and Item Response Theory analysis. Three additional demographic variables (sex, education, and age) are also included.

Exercises

Exercise 1: Your first Quarto document

  • Create a new Quarto HTML document.
  • Fill in the YAML options with a title and author.
  • Render your document.
  • Change the format to docx.
  • Re-render your document.
  • What about PDF?
Solution
exercise1.qmd
---
title: "My Document"
author: "Nicola Rennie"
format: pdf
---

## Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

## Running Code

When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

```{r}
1 + 1
```

You can add options to executable code like this 

```{r}
#| echo: false
2 * 2
```

The `echo: false` option disables the printing of code (only output is displayed).

Exercise 2: Document content

  • Create level 1 and level 2 section headings in your document.
  • Add some text into your document. If you don’t want to write your own, there is some sample text in sample-text.qmd.
  • Format the text to include a list, and bold or italic text.
  • Add a link to the International Personality Item Pool: https://ipip.ori.org/.
  • Include an image in your document. Find your own or use eggs.jpg.
  • Add a caption to the image and center align it.
  • Render your document - check the image appears and the link works.

You can download the image file here (or find your own!): eggs.jpg

Solution
exercise2.qmd
---
title: "My Document"
author: "Nicola Rennie"
format: html
---

# 25 Personality items representing 5 factors

## Description

25 personality self report items taken from the International Personality Item Pool ([ipip.ori.org](https://ipip.ori.org/)) were included as part of the **Synthetic Aperture Personality Assessment (SAPA)** web based personality assessment project. The data from 2800 subjects are included here as a demonstration set for scale construction, factor analysis, and Item Response Theory analysis.

Three additional demographic variables are also included:

* sex
* education
* age

```{r}
x <- 5
x
```

![Eggs experiencing a variety of emotions](eggs.jpg){fig-align="center" width="50%"}

Exercise 3: Code blocks

  • Add a code block that loads the {psych} package and a dataset from the package e.g. bfi.
  • Add another code block that makes a plot e.g. a bar chart of participants’ education categories, education. You can use either base R or {ggplot2} (or another package!).
  • Change the code block options and see what happens!
  • Add a third code block that creates a table showing the first 10 rows and first 5 columns of your data. Hint: head(bfi[, 1:5], 10). Use any table package you prefer.
Solution
exercise3.qmd
---
title: "My Document"
author: "Nicola Rennie"
format: html
execute: 
  echo: false
---

# 25 Personality items representing 5 factors

## Description

25 personality self report items taken from the International Personality Item Pool ([ipip.ori.org](https://ipip.ori.org/)) were included as part of the **Synthetic Aperture Personality Assessment (SAPA)** web based personality assessment project. The data from 2800 subjects are included here as a demonstration set for scale construction, factor analysis, and Item Response Theory analysis.

Three additional demographic variables are also included:

* sex
* education
* age

![Eggs experiencing a variety of emotions](eggs.jpg){fig-align="center"}

## Data exploration

```{r}
#| label: load-data
library(psych)
data(bfi)
```

We see a bar chart of the gender and education level of participants below:

```{r}
#| label: gen-edu-barplot
#| fig-width: 5
#| fig-height: 5
#| fig-cap: "Education and gender (Males = 1, Females = 2) of participants."
# Add a bar plot of gender and education
barplot(
  table(bfi$gender, bfi$education),
  beside = TRUE,
  legend = TRUE
)
```

A table of agreeableness scores for the first 10 participants is shown below:

```{r}
#| label: sample-table
library(tinytable)
tt(head(bfi[, 1:5], 10))
```

Exercise 4: Document referencing

  • Update (or add!) a label to the figure code block you created earlier.
  • Add a reference to this in the text.
  • Add section references, and cross reference to this section somewhere else in the document.
  • Add a citation by either:
    • creating a .bib file and listing it in the YAML.
    • using the Zotero integration
Solution
exercise4.qmd
---
title: "My Document"
author: "Nicola Rennie"
format: html
bibliography: references.bib
number-sections: true
execute: 
  echo: false
---

# 25 Personality items representing 5 factors

Data exploration will be discussed in @sec-data-exp.

## Description

25 personality self report items taken from the International Personality Item Pool ([ipip.ori.org](https://ipip.ori.org/)) were included as part of the **Synthetic Aperture Personality Assessment (SAPA)** web based personality assessment project. The data from 2800 subjects are included here as a demonstration set for scale construction, factor analysis, and Item Response Theory analysis.

Three additional demographic variables are also included:

-   sex
-   education
-   age

![Eggs experiencing a variety of emotions](eggs.jpg){fig-align="center"}

## Data exploration {#sec-data-exp}

Here we explore `bfi` data in the {psych} package [@psych].

```{r}
#| label: load-data
library(psych)
data(bfi)
```

We see a bar chart of the gender and education level of participants below in @fig-gen-edu-barplot.

```{r}
#| label: fig-gen-edu-barplot
#| fig-width: 5
#| fig-height: 5
#| fig-cap: "Education and gender (Males = 1, Females = 2) of participants."
# Add a bar plot of gender and education
barplot(
  table(bfi$gender, bfi$education),
  beside = TRUE,
  legend = TRUE
)
```

A table of agreeableness scores for the first 10 participants is shown below:

```{r}
#| label: sample-table
library(tinytable)
tt(head(bfi[, 1:5], 10))
```
references.bib
@Manual{psych,
  title = {psych: Procedures for Psychological, Psychometric, and Personality Research},
  author = {{William Revelle}},
  organization = {Northwestern University},
  address = {Evanston, Illinois},
  year = {2024},
  note = {R package version 2.4.1},
  url = {https://CRAN.R-project.org/package=psych},
}

Exercise 5: Formatting documents

  • Install the APA Quarto extension.
  • Change the output format of your document to APA (docx, html, or pdf).
  • Re-render your document. Do you get an error?
  • Look at the installation instructions at wjschne.github.io/apaquarto/installation.html to see what other YAML options are added.
  • Add the required information about the author (you).
  • Re-render your document.
Solution
exercise5.qmd
---
title: "My Document"
shorttitle: "My Paper's Title"
author:
  - name: Jane Doe
    corresponding: true
    orcid: 0000-0000-0000-0000
    email: janedoe@email.com
    affiliations:
      - name: University
        department: My Department
        address: 62 West Wallaby Street
        city: Wigan
        region: Lancashire
        postal-code: WN6 0DOG
abstract: "This is my abstract."
keywords: [keyword1, keyword2]
author-note:
  disclosures:
    conflict of interest: The author has no conflict of interest to declare.
bibliography: references.bib
execute: 
  echo: false
---

# 25 Personality items representing 5 factors

Data exploration will be discussed in @sec-data-exp.

## Description

25 personality self report items taken from the International Personality Item Pool ([ipip.ori.org](https://ipip.ori.org/)) were included as part of the **Synthetic Aperture Personality Assessment (SAPA)** web based personality assessment project. The data from 2800 subjects are included here as a demonstration set for scale construction, factor analysis, and Item Response Theory analysis.

Three additional demographic variables are also included:

* sex
* education
* age

![Eggs experiencing a variety of emotions](eggs.jpg){fig-align="center"}

## Data exploration {#sec-data-exp}

Here we explore `bfi` data in the {psych} package [@psych].

```{r}
#| label: load-data
library(psych)
data(bfi)
```

We see a bar chart of the gender and education level of participants below in @fig-gen-edu-barplot.

```{r}
#| label: fig-gen-edu-barplot
#| fig-width: 5
#| fig-height: 5
#| fig-cap: "Education and gender (Males = 1, Females = 2) of participants."
# Add a bar plot of gender and education
barplot(
  table(bfi$gender, bfi$education),
  beside = TRUE,
  legend = TRUE
)
```

A table of agreeableness scores for the first 10 participants is shown below:

```{r}
#| label: sample-table
library(tinytable)
tt(head(bfi[, 1:5], 10))
```
references.bib
@Manual{psych,
  title = {psych: Procedures for Psychological, Psychometric, and Personality Research},
  author = {{William Revelle}},
  organization = {Northwestern University},
  address = {Evanston, Illinois},
  year = {2024},
  note = {R package version 2.4.1},
  url = {https://CRAN.R-project.org/package=psych},
}

In the same folder as exercise5.qmd there should be the eggs.jpg image and another folder called _extensions/wjschne/apaquarto.