-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathREADME.qmd
More file actions
164 lines (124 loc) · 4.79 KB
/
README.qmd
File metadata and controls
164 lines (124 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
---
format: gfm
execute:
echo: false
warning: false
---
<img src='logo/Hex.png' align="right" height="139" />
```{r}
#| include: false
library(tidyverse)
library(glue)
extract_pkgs <- function(fl) {
if (length(fl) == 1) {
txt <- read.delim(fl, header = FALSE)[[1]]
pkgs <- str_extract_all(
string = txt,
pattern = "((?<=(library|require)\\()[a-zA-Z0-9\\.]{1,}(?=\\))|[a-zA-Z0-9\\.]{1,}(?=\\:{2}))"
)
pkgs <- unlist(pkgs)
} else if (length(fl) > 1) {
pkgs <- sapply(fl, extract_pkgs)
}
pkgs |>
unlist(recursive = TRUE) |>
unique()
}
```
# Practical Applications in R for Psychologists
[](https://creativecommons.org/about/program-areas/education-oer/)
[](http://creativecommons.org/licenses/by-nc/4.0/)
[](http://cran.r-project.org/)
<sub>*Last updated `r Sys.Date()`.*</sub>
This Github repo contains all lesson files for *Practical Applications in R for Psychologists*.
The goal is to impart students with the basic tools to process data, describe data (w/ summary statistics and plots),
and the foundations of **building, evaluating and comparing statistical models in `R`**,
focusing on linear regression modeling (using both frequentist and Bayesian approaches).
These topics were taught in the graduate-level course ***Advanced Research Methods for Psychologists*** (Psych Dep., Ben-Gurion University of the Negev), laying the foundation for the following topic-focused courses:
- [Structural equation modelling (*SEM*)](https://github.com/mattansb/Structural-Equation-Modeling-foR-Psychologists)
- [Machine Learning (*ML*)](https://github.com/mattansb/Machine-Learning-foR-Psychologists)
- [Hierarchical linear models (*HLM*)](https://github.com/mattansb/Hierarchical-Linear-Models-foR-Psychologists)
- ~~[Analysis of Factorial Designs (*ANOVA*)](https://github.com/mattansb/Analysis-of-Factorial-Designs-foR-Psychologists)~~ (no longer offered)
**Notes:**
- This repo contains only materials relating to *Practical Applications in R*. Though statistics are naturally discussed in many lessons, the focus is generally on the application and not on the theory.
- Please note that some code does not work *on purpose* and without warning, to force students to learn to debug.
## Setup
You will need:
1. A fresh installation of [**`R`**](https://cran.r-project.org/) (preferably version 4.5.0 or above).
2. [RStudio IDE](https://www.rstudio.com/products/rstudio/download/) or [Positron](https://positron.posit.co/download) (optional, but recommended).
3. The following packages, listed by lesson:
```{r}
r_files <- list.files(pattern = ".R$", recursive = TRUE, full.names = TRUE)
r_files <- r_files[str_detect(r_files, "^\\./\\d{2}")]
r_lesson_names <- str_extract(r_files, pattern = "(?<=(/)).{1,}(?=(/))")
r_list <- split(r_files, r_lesson_names)
r_pkgs <- lapply(r_list, extract_pkgs)
r_df <- tibble(
Lesson = names(r_pkgs),
Packages = r_pkgs
)
r_df |>
mutate(
Lesson = glue("[{Lesson}]({Lesson}//)"),
Packages = sapply(Packages, \(x) glue('`{x}`') |> glue_collapse(sep = ", "))
) |>
knitr::kable()
```
<details><summary><i>Installing R Packages</i></summary>
You can install all the R packages used by running:
```{r}
r_pkgs <- r_pkgs |>
unlist(recursive = TRUE) |>
unique() |>
setdiff(tidyverse::tidyverse_packages()) |>
setdiff(easystats::easystats_packages()) |>
c("tidyverse", "easystats") |>
sort()
packinfo <- installed.packages(fields = c("Package", "Version"))
get_src <- function(pkg) {
pd <- packageDescription(pkg)
if (is.null(src <- pd$Repository)) {
if (!is.null(src <- pd$GithubRepo)) {
src <- paste0("Github: ", pd$GithubUsername, "/", src)
} else {
src <- "Dev"
}
}
return(src)
}
r_pkg_info <- tibble(
pkg = r_pkgs,
source = sapply(r_pkgs, get_src),
version = packinfo[r_pkgs, "Version"]
) |>
filter(version != packageVersion("base")) |>
mutate(
pak_src = case_when(
source == "Dev" ~ NA,
source == "CRAN" ~ "cran",
str_detect(source, "Github") ~ "github",
str_detect(source, "Bioconductor") ~ "bioc"
),
pak = case_match(
pak_src,
"cran" ~ glue("cran::{pkg}"),
"bioc" ~ glue("bioc::{pkg}"),
"github" ~ glue("github::{str_remove_all(source, 'Github: ')}"),
.default = pkg
)
) |>
arrange(pkg)
cat("# in alphabetical order:\n\n")
cat("pak::pak(\n c(")
r_pkg_info$comma <- rep(",", nrow(r_pkg_info))
r_pkg_info$comma[nrow(r_pkg_info)] <- ""
cat(
glue_data(r_pkg_info, ' "{pak}"{comma} # {version}') |>
glue_collapse(sep = "\n")
)
cat(" )\n)")
```
</details>
---
### Acknowledgements
Materials developed with Kim Rubinstein and Lihi Nagar.