Skip to content

Commit

Permalink
small changes to ch 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gabymahrholz committed Sep 26, 2024
1 parent ce35e66 commit d9f1a8c
Show file tree
Hide file tree
Showing 8 changed files with 674 additions and 172 deletions.
2 changes: 1 addition & 1 deletion .Rproj.user/EEE9B81B/pcs/source-pane.pper
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"activeTab": 2
"activeTab": 0
}
8 changes: 3 additions & 5 deletions .quarto/_freeze/02-wrangling/execute-results/html.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .quarto/cites/index.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"11-multiple-regression.qmd":[],"05-dataviz2.qmd":[],"09-simple-regression.qmd":[],"12-factorial-anova.qmd":[],"06-paired.qmd":[],"08-paired.qmd":[],"instructions.qmd":["usethis"],"appendix-a-installing-r.qmd":[],"appendix-c-exporting-server.qmd":[],"07-independent.qmd":[],"09-correlation.qmd":[],"05-chi-square-one-sample.qmd":[],"01-basics.qmd":[],"04-dataviz.qmd":[],"10-multiple-regression.qmd":[],"04-dataviz2.qmd":[],"index.qmd":[],"02-wrangling.qmd":[],"webexercises.qmd":[],"13-factorial-anova.qmd":[],"05-independent.qmd":[],"appendix-x-How-to-cite-R.qmd":[],"appendix-b-updating-packages.qmd":[],"04-prob-binom-one-sample.qmd":[],"11-one-way-anova.qmd":[],"07-paired.qmd":[],"references.qmd":[],"10-regression.qmd":[],"appendix-y-license.qmd":[],"03-wrangling2.qmd":[],"06-independent.qmd":[],"12-one-way-anova.qmd":[],"08-correlation.qmd":[],"04-chi-square-one-sample.qmd":[],"06-chi-square-one-sample.qmd":[],"07-apes.qmd":[],"03-dataviz.qmd":[],"appendix-d-symbols.qmd":[]}
{"09-correlation.qmd":[],"09-simple-regression.qmd":[],"05-dataviz2.qmd":[],"07-apes.qmd":[],"11-one-way-anova.qmd":[],"11-multiple-regression.qmd":[],"06-paired.qmd":[],"08-paired.qmd":[],"appendix-c-exporting-server.qmd":[],"06-independent.qmd":[],"04-dataviz.qmd":[],"10-regression.qmd":[],"references.qmd":[],"04-dataviz2.qmd":[],"index.qmd":[],"13-factorial-anova.qmd":[],"01-basics.qmd":[],"08-correlation.qmd":[],"02-wrangling.qmd":[],"12-one-way-anova.qmd":[],"appendix-d-symbols.qmd":[],"10-multiple-regression.qmd":[],"03-wrangling2.qmd":[],"05-independent.qmd":[],"instructions.qmd":["usethis"],"03-dataviz.qmd":[],"appendix-a-installing-r.qmd":[],"appendix-x-How-to-cite-R.qmd":[],"07-independent.qmd":[],"06-chi-square-one-sample.qmd":[],"04-chi-square-one-sample.qmd":[],"appendix-b-updating-packages.qmd":[],"05-chi-square-one-sample.qmd":[],"12-factorial-anova.qmd":[],"webexercises.qmd":[],"07-paired.qmd":[],"appendix-y-license.qmd":[],"04-prob-binom-one-sample.qmd":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/7a0d69cd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[{"order":{"section":[2,0,0,0,0,0,0],"number":1},"key":"sec-wrangling"},{"caption":"2.4 Activity 4: Questionable Research Practices (QRPs)","order":{"section":[2,4,0,0,0,0,0],"number":1},"key":"sec-ch2_act4"}],"options":{"chapter-id":"sec-wrangling","chapters":true},"headings":["intended-learning-outcomes","individual-walkthrough","activity-1-setup","activity-2-load-in-the-libraries-and-read-in-the-data","activity-3-calculating-demographics","for-the-full-sample-using-summarise","fixing-age","computing-summary-stats","computing-summary-stats---third-attempt","per-gender-using-summarise-and-group_by","adding-percentages","sec-ch2_act4","the-main-goal-is-to-compute-the-mean-qrp-score-per-participant-for-time-point-1.","activity-5-knitting","activity-6-export-a-data-object-as-a-csv","pair-coding","task-1-open-the-r-project-you-created-last-week","task-2-open-your-.rmd-file-from-last-week","task-3-load-in-the-library-and-read-in-the-data","task-4-calculating-the-mean-for-flourishing_pre","test-your-knowledge-and-challenge-yourself","knowledge-check","question-1","question-2","question-3","question-4","question-5","error-mode","question-6","question-7","question-8","challenge-yourself","sec-wrangling"]}
{"entries":[{"key":"sec-wrangling","order":{"section":[2,0,0,0,0,0,0],"number":1}},{"caption":"2.4 Activity 4: Questionable Research Practices (QRPs)","key":"sec-ch2_act4","order":{"section":[2,4,0,0,0,0,0],"number":1}}],"headings":["intended-learning-outcomes","individual-walkthrough","activity-1-setup","activity-2-load-in-the-libraries-and-read-in-the-data","activity-3-calculating-demographics","for-the-full-sample-using-summarise","fixing-age","computing-summary-stats","computing-summary-stats---third-attempt","per-gender-using-summarise-and-group_by","adding-percentages","sec-ch2_act4","the-main-goal-is-to-compute-the-mean-qrp-score-per-participant-for-time-point-1.","activity-5-knitting","activity-6-export-a-data-object-as-a-csv","pair-coding","task-1-open-the-r-project-you-created-last-week","task-2-open-your-.rmd-file-from-last-week","task-3-load-in-the-library-and-read-in-the-data","task-4-calculating-the-mean-for-flourishing_pre","test-your-knowledge-and-challenge-yourself","knowledge-check","question-1","question-2","question-3","question-4","question-5","error-mode","question-6","question-7","question-8","challenge-yourself","sec-wrangling"],"options":{"chapter-id":"sec-wrangling","chapters":true}}
33 changes: 27 additions & 6 deletions 02-wrangling.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,25 @@ demo_by_gender

#### The main goal is to compute the mean QRP score per participant for time point 1. {.unnumbered}

At the moment, the data is in wide format. The table below shows data from the first 3 participants:

```{r}
head(data_prp, n = 3)
```
<p></p>

Looking at the QRP data at time point 1, you determine that

* individual item columns are `r mcq(c(answer = "numeric", x = "character"))`, and
* according to the codebook, there are `r mcq(c(answer = "no", x = "some"))` reverse-coded items in this questionnaire.

So, we just have to **compute an average score for items 1 to 11** as items 12 to 15 are distractor items. Seems quite straightforward.
According to the codebook and the data table above, we just have to **compute the average score for QRP items `r fitb("1")` to `r fitb("11")`**, since items `r fitb("12")` to `r fitb("15")` are distractor items. Seems quite straightforward.

The downside is that individual items are each in a separate column, i.e., in **wide format**, and everything would be easier if the items were arranged in **long format**.
However, as you can see in the table above, each item is in a separate column, meaning the data is in **wide format**. It would be much easier to calculate the mean scores if the items were arranged in **long format**.


Let’s tackle this problem step by step. It’s best to create a separate data object for this. If we tried to compute it within `data_prp`, it could quickly become messy.

Let's tackle this problem in steps. Best would be to create a separate data object for that. If we wanted to compute this within `data_prp`, it would turn into a nightmare.

* **Step 1**: Select the relevant columns `Code`, and `QRPs_1_Time1` to `QRPs_1_Time1` and store them in an object called `qrp_t1`
* **Step 2**: Pivot the data from wide format to long format using `pivot_longer()` so we can calculate the average score more easily (in step 3)
Expand All @@ -283,7 +292,7 @@ qrp_t1 <- data_prp %>%
# Step 2
pivot_longer(cols = -Code, names_to = "Items", values_to = "Scores") %>%
# Step 3
group_by(Code) %>% # grouping py participant id
group_by(Code) %>% # grouping by participant id
summarise(QRPs_Acceptance_Time1_mean = mean(Scores)) %>% # calculating the average Score
ungroup() # just make it a habit
```
Expand Down Expand Up @@ -479,7 +488,8 @@ dog_data_raw <- read_csv("???")

### Task 4: Calculating the mean for `Flourishing_pre` {.unnumbered}

* **Step 1**: Select all relevant columns, including participant ID and all 8 items from the `Flourishing` questionnaire completed before the intervention. Store this data in an object called `data_flourishing`.

* **Step 1**: Select all relevant columns from `dog_data_raw`, including participant ID and all items from the `Flourishing` questionnaire completed before the intervention. Store this data in an object called `data_flourishing`.


::: {.callout-note collapse="true" icon="false"}
Expand All @@ -500,6 +510,11 @@ From the codebook, we know that:
* The participant ID column is called `RID`.
* The Flourishing items at the pre-intervention stage start with `F1_`.

```{r eval=FALSE}
data_flourishing <- ??? %>%
select(???, F1_???:F1_???)
```

:::

:::
Expand All @@ -524,6 +539,8 @@ We also need 3 arguments in that function:

## More concrete hint

We need `pivot_longer()`. You already encountered `pivot_longer()` in first year (or in the individual walkthrough if you have already completed this Chapter). The 3 arguments was also a give-away; `pivot_wider()` only requires 2 arguments.

```{r eval=FALSE}
pivot_longer(cols = ???, names_to = "???", values_to = "???")
```
Expand All @@ -545,16 +562,20 @@ Before summarising the mean, you may need to group the data.

## More concrete hint

To compute an average score **per participant**, we would need to group by participant ID first.

```{r eval=FALSE}
group_by(???) %>%
summarise(Flourishing_pre = ???(???)) %>%
summarise(Flourishing_pre = mean(???)) %>%
ungroup()
```
:::

:::




::: {.callout-caution collapse="true" icon="false"}

## Solution
Expand Down
8 changes: 3 additions & 5 deletions _freeze/02-wrangling/execute-results/html.json

Large diffs are not rendered by default.

Loading

0 comments on commit d9f1a8c

Please sign in to comment.