Skip to content

Commit

Permalink
Add renv for reproducibility (#1)
Browse files Browse the repository at this point in the history
* removed scratch folder

* Delete wordcount.R

* Add renv and snapshot packages

* Update README.md

* removed reference to recalibration data. Using FangPsychometric package instead

* updated references and repo url

* added bookdown site

* added arm and rstanarm to renv
  • Loading branch information
adknudson authored Apr 11, 2024
1 parent de5c061 commit 1d65c07
Show file tree
Hide file tree
Showing 94 changed files with 17,581 additions and 742 deletions.
1 change: 1 addition & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ _bookdown_files

presentation/*_cache
presentation/*_files

libs/
3 changes: 3 additions & 0 deletions .renvignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docs/
_bookdown_files/
latex/
154 changes: 14 additions & 140 deletions 200-appendix.Rmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
```{r ch200-Setup, include=FALSE}
# load these libraries so that renv snapshots them
# and so they can be used in the bibliography
library(rstanarm)
library(arm)
```


# (APPENDIX) Appendix {-}


Expand Down Expand Up @@ -148,145 +156,6 @@ The final round of reparameterization came in the form of adopting non-centered
# Reproducible Results {#reproduce}


```{r ch230-setup, include=FALSE}
library(tidyverse)
fp <- list.files(path = "data/RecalibrationData/",
recursive = TRUE,
pattern = "(.mat)$")
fp_typ_reg <-
"^(\\w+)/(\\w+)/(\\w+)/[A-Z]{2,3}_*[A-Z]*(adapt[0-9]|baseline[0-9]*).*"
fp_typ_logi <- str_detect(fp, fp_typ_reg)
fp_typ <- fp[fp_typ_logi]
fp_atyp <- fp[!fp_typ_logi]
get_feat_typ <- function(mat) {
expr <-
"^(\\w+)/(\\w+)/(\\w+)/[A-Z]{2,3}_*[A-Z]*(adapt[0-9]|baseline[0-9]*).*"
str_replace(mat, expr, replacement = "\\1 \\2 \\3 \\4") %>%
str_split(pattern = " ", simplify = TRUE) %>% c()
}
get_feat_atyp <- function(mat) {
expr <-
"^(\\w+)/(\\w+)/(\\w+)/[A-Zb-z]{2,3}_{0,1}[0-9]*_{0,1}(\\S*)__MAT.mat"
str_replace(mat, expr, "\\1 \\2 \\3 \\4") %>%
str_split(" ", simplify = TRUE) %>% c()
}
feat_typ <- map(fp_typ, get_feat_typ) %>% do.call(what = rbind)
feat_atyp <- map(fp_atyp, get_feat_atyp) %>% do.call(what = rbind)
cn <- c("task", "age_group", "initials", "trial")
colnames(feat_typ) <- cn
colnames(feat_atyp) <- cn
feat_typ <- as_tibble(feat_typ) %>%
add_column(path = fp_typ, .before = 1) %>%
mutate(trial = if_else(str_detect(trial, "baseline"), "baseline", trial))
feat_atyp <- as_tibble(feat_atyp) %>%
add_column(path = fp_atyp, .before = 1) %>%
mutate(trial = if_else(trial == "", "baseline", trial)) %>%
mutate(trial = str_replace(trial, "([A-Za-z]+)_*([0-9])+", "adapt\\2")) %>%
mutate(trial = if_else(str_detect(trial, "adapt"), trial, "baseline"))
feature_tbl <- bind_rows(feat_typ, feat_atyp) %>%
mutate(
task = tolower(task),
age_group = factor(age_group),
age_group = recode_factor(
age_group,
Young = "young_adult",
MiddleAge = "middle_age",
Older = "older_adult",
),
age_group = as.character(age_group)
)
age_sex_tbl <- readxl::read_xlsx(
"data/RecalibrationData/ParticipantAgeSex.xlsx",
col_types = c("guess", "numeric", "guess")
) %>%
mutate_at(vars(Sex), as.factor) %>%
rename(initials = ID,
age = Age,
sex = Sex) %>%
arrange(initials)
get_age_group <- function(age) {
age_group <- vector("character", length(age))
age_group[age >= 18 & age <= 30] <- "young_adult"
age_group[age >= 39 & age <= 50] <- "middle_age"
age_group[age >= 65 & age <= 75] <- "older_adult"
age_group[age_group == ""] = NA
age_group
}
feature_tbl <- feature_tbl %>%
unite(col = "initials_age_group",
initials,
age_group,
sep = "-",
remove = TRUE)
age_sex_tbl <- age_sex_tbl %>%
mutate(age_group = get_age_group(age)) %>%
unite(col = "initials_age_group",
initials,
age_group,
sep = "-",
remove = TRUE)
feature_tbl <-
full_join(feature_tbl, age_sex_tbl, by = "initials_age_group") %>%
separate(initials_age_group, c("initials", "age_group"), sep = "-") %>%
mutate(age_group = factor(
age_group,
levels = c("young_adult", "middle_age", "older_adult"),
ordered = FALSE
)) %>%
mutate(trial = factor(
trial,
levels = c("baseline", "adapt1", "adapt2", "adapt3"),
ordered = FALSE
))
features <- feature_tbl %>%
mutate(
tmp_task = recode_factor(
task,
audiovisual = "av",
visual = "vis",
sensorimotor = "sm",
duration = "dur"
),
trial = recode_factor(
trial,
baseline = "pre",
adapt1 = "post1",
adapt2 = "post2",
adapt3 = "post3"
),
initials = str_replace(initials, "JM_F", "JM"),
tmp_sex = tolower(as.character(sex)),
tmp_age_group = recode_factor(
age_group,
young_adult = "Y",
middle_age = "M",
older_adult = "O"
)
) %>%
unite(
rid,
tmp_task,
trial,
tmp_age_group,
tmp_sex,
initials,
sep = "-",
remove = FALSE
) %>%
unite(sid,
tmp_age_group,
tmp_sex,
initials,
sep = "-",
remove = FALSE) %>%
select(-c(tmp_task, tmp_sex, tmp_age_group, initials)) %>%
select(rid, sid, path, task, trial, age_group, age, sex) %>%
arrange(rid) %>%
mutate(rid = factor(rid),
sid = factor(sid))
```


Data doesn't always come in a nice tidy format, and we had to turn the raw experimental data into a clean data set that is ready for modeling. Sometimes the process is quick and straight forward, but other times, like with this psychometric data, it takes more effort and clever techniques. There is academic value in describing the steps taken to reduce the headache later.


Expand Down Expand Up @@ -338,7 +207,12 @@ Since there is only a handful of irregular block names, they can be dealt with b

\setstretch{1.0}
```{r ch230-Remote Monkey, echo=TRUE}
glimpse(features, width = 60)
library(dplyr)
FangPsychometric::multitask |>
select(rid, sid, task, block, age_group, age, sex) |>
distinct() |>
arrange(rid) |>
glimpse(width = 60)
```
\setstretch{2.0}

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Alex Knudson - Masters Thesis
# A Bayesian Multilevel Model for the Psychometric Function using R and Stan

The HTML version of my thesis can be viewed offline by cloning an earlier version of this repo and opening the docs folder. The folder was removed in the latest commit due to vulnerabilities of JQuery, and me no longer being able to render new versions of the thesis using newer versions of libraries on which my code depends on. I do not have the energy to maintain this code, nor to put in the effort to figure out which versions of libraries would work to keep the code running. In short, learn from my mistake and use `renv` early on to ensure that your projects maintain reproducibility ;D
The PDF version of my thesis can be [found here](https://github.com/adknudson/UNR-Masters-Thesis/blob/v4.0/docs/adknudson-thesis.pdf).

This thesis is created within an Rproject that uses `renv` to maintain reproducibility. This package also utilizes private research data that can be provided as an R tarball upon request.
4 changes: 2 additions & 2 deletions _bookdown.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
book_filename: "adknudson-thesis"
delete_merged_file: yes
repo: https://github.com/adknudson/thesis/
repo: https://github.com/adknudson/UNR-Masters-Thesis/
before_chapter_script: [_common.R]
after_chapter_script: [_outgoing.R]
view: https://github.com/adknudson/thesis/blob/master/%s
view: https://github.com/adknudson/UNR-Masters-Thesis/blob/master/%s
rmd_files: [
"index.Rmd",
"030-methods.Rmd",
Expand Down
Loading

0 comments on commit 1d65c07

Please sign in to comment.