https://github.com/favstats/openmindr
This is a helper package for the work at OpenMind
https://github.com/favstats/openmindr
Last synced: 11 months ago
JSON representation
This is a helper package for the work at OpenMind
- Host: GitHub
- URL: https://github.com/favstats/openmindr
- Owner: favstats
- Created: 2019-03-03T03:11:43.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-30T09:28:51.000Z (about 7 years ago)
- Last Synced: 2025-04-02T05:12:16.731Z (about 1 year ago)
- Language: R
- Homepage:
- Size: 206 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
title: "openmindR"
output: github_document
---
Install package like this:
```{r setup, eval=FALSE}
devtools::install_github("favstats/openmindR")
```
```{r, echo=F}
knitr::opts_chunk$set(message = F,
warning = F)
```
Load package(s):
```{r}
library(openmindR)
library(tidyverse)
```
## OpenMind Cleaning Functions
```{r, echo = F}
library(DBI)
library(magrittr)
library(glue)
library(lubridate)
db_get_data <- function(tbl_dat) {
con <- dbConnect(RSQLite::SQLite(), "../om_metrics_report/sql_data/omdata.db")
out <- con %>%
tbl(tbl_dat) %>%
collect()
dbDisconnect(con)
return(out)
}
## Matching string for all Q variables
q_strings <- paste0(
paste0("Q", 1:18, "P", collapse = "|"), "|", paste0("Q", 1:18, "F", collapse = "|")
)
## Matching string for all (relevant) D variables
d_strings <- paste0("D", 1, collapse = "|")
## Matching string for all C variables
c_strings <- paste0("C", 1:3, collapse = "|")
## Matching string for all (relevant) D, Q and C variables
var_strings <- paste0(q_strings, "|", d_strings, "|", c_strings, collapse = "|")
## Matching string for all Q and C variables
q_c_strings <- paste0(q_strings, "|", c_strings, collapse = "|")
## Matching string for ranging vars from 0 to 1
range01_strings <- str_c(str_c("Q", 3:12, "P", collapse = "|"), "|",
str_c("Q", 3:12, "F", collapse = "|"),
str_c("|", c_strings, collapse = "|")
)
# Read in data
dat.acc <- db_get_data("dat.acc")
dat.par <- db_get_data("dat.par")
dat.ass4 <- db_get_data("dat.ass4")
dat.ass5 <- db_get_data("dat.ass5")
dat.ass <- dat.ass4 %>%
rename_at(vars(matches("Followup")), ~str_replace(., "Followup", "FollowUp")) %>%
bind_rows(dat.ass5)
```
## `om_filter_data`
Filter down Assessment data from AirTable by `AssessmentsDone`, `AssessmentVersion` and `AccessCodes`.
```{r}
dat.ass %>%
# specify which number of assessment you want to have
om_filter_data(n_assessments = 1:3,
# assessment version?
version = 4,
# select Accesscode(s) to produce report for
accesscode = "Wilkes"
# "Wilkes" #try this out :)
)
```
This dataset was filtered down to only AccessCodes that include "Wilkes". The `accesscode` argument is not case-sensitive and can both be used with vectors:
```{r}
dat.ass %>%
# specify which number of assessment you want to have
om_filter_data(n_assessments = 1:3,
# assessment version?
version = 4,
# select Accesscode(s) to produce report for
accesscode = c("SuszkoWilkesUF18", "KarimiWilkesUF18")
)
```
And individual strings:
```{r}
dat.ass %>%
# specify which number of assessment you want to have
om_filter_data(n_assessments = 1:3,
# assessment version?
version = 4,
# select Accesscode(s) to produce report for
accesscode = c("suszko|karimi")
)
```
## `om_clean_par`
Cleans up ParticipantProgress data and creates several measures:
+ **StepTimes1 to StepTimes5:** Duration in minutes to complete a step
+ **StepCorrect1 to StepCorrect5:** Percentage of correct answers for each step
+ **FeedbackAnswers:** Q1 to Q5 for each individual step
```{r}
dat.par %>%
om_clean_par()
```
## `om_construct_measures`
This is a higher-level function that uses both `polar_measures` and `calc_ih` to constuct various measures.
Creates the following variables:
+ **Q14:** Affective Polarization
+ **Q15:** Ingroup
+ **Q16:** Outgroup
+ **Q17:** Ingroup vs. Outgroup Affective Polarization
+ **Q18:** Intellectual Humility
Function automatically accounts for Assessment Version 4 and 5/5.1.
```{r, eval=F}
dat.ass %>%
om_construct_measures()
```
> Error in polar_measures(., Q1Pre, Q2Pre) : Input data is missing column `ppol_cat`. Please make sure to run om_clean_ppol before you run om_construct_measures.
Uh oh! That didn't work! `om_construct_measures` needs the column `ppol_cat` to run which can be created with the function `om_clean_ppol`.
## `om_clean_ppol`
Creates the following measures of Political Orientation
+ **ppol_raw:** a variable that merges Assessment V4 and V5.1 spelling of Political Orientation (D4)
+ **ppol:** a factor variable ordered from "Very Progressive/left" to "Very Conservative/right". Excludes all other categories as NA (classical liberal etc.)
+ **ppol_num:** numeric variable ranging from 1 "Very Progressive/left" to 7 "Very Conservative/right"
+ **ppol_cat:** a factor variable which has two categories "Progressive" and "Conservative". The rest is NA.
```{r}
dat.ass <- dat.ass %>%
om_clean_ppol()
dat.ass
```
Now `om_construct_measures` will work!
```{r}
dat.ass %>%
om_construct_measures()
```
## `remove_dups`
This function is really important to clean up duplicated OMIDs that occasionally occur within AirTable.
```{r, message = T}
dat.ass %>%
remove_dups()
```
## OpenMind ggplot2 theme
There are three functions for the ggplot2 theme:
+ `theme_om`
+ `scale_fill_om`
+ `scale_color_om`
Make sure you have the Poppins font installed!
```{r}
windowsFonts(`Poppins` = windowsFont("Poppins"))
```
[Good tutorial on how to install custom fonts in R](https://www.andrewheiss.com/blog/2017/09/27/working-with-r-cairo-graphics-custom-fonts-and-ggplot/)
**Example**
```{r fig.width=8, fig.height=5, message=F, warning=F}
## Load tidyverse
library(tidyverse)
titanic_dat <- Titanic %>% as_tibble()
titanic_dat %>%
ggplot(aes(Sex, n)) +
geom_col(aes(fill = Class), position = position_dodge()) +
theme_om(legend_position = c(0.9, 0.75)) +
scale_fill_om("Class") +
facet_wrap(~Survived) +
labs(title = "Titanic Survival by Age and Class")
```
**Adapt `theme_om`**
+ `legend_position`
+ `axis_text_size`
+ `axis_title_size`
+ `legend_text_size`
+ `title_size`
```{r fig.width=8, fig.height=5, message=F}
titanic_dat %>%
ggplot(aes(Class, n, fill = Class)) +
geom_col() +
theme_om(legend_position = "bottom",
axis_text_size = 10,
axis_title_size = 15,
legend_text_size = 10,
title_size = 20) +
scale_fill_om() +
facet_wrap(~Survived) +
labs(title = "Titanic Survival by Class")
```
Or all text sizes at once
+ `overall_text_size`
```{r fig.width=8, fig.height=5, message=F}
titanic_dat %>%
ggplot(aes(Class, n, fill = Class)) +
geom_col() +
theme_om(legend_position = "top",
overall_text_size = 15) +
scale_fill_om() +
facet_wrap(~Survived) +
labs(title = "Titanic Survival by Class")
```