---
title: "Defense Against the Dark Rs"
subtitle: "Template Document"
author: "Dustin Haraden, PhD"
date: "`r Sys.Date()`"
output:
  html_document:
    toc: true
    toc_float: true
    code_folding: hide
editor_options:
  chunk_output_type: console
  markdown:
    wrap: 72
---

# Getting Started

## Useful Resources

For just getting started with R, here are some of my suggestions:

[R for Data Science](https://r4ds.hadley.nz/)

[R for the Rest of Us: A Stats Free
Introduction](https://book.rfortherestofus.com/)

[Learning Stats with R](https://learningstatisticswithr.com/) (An Online
version [here](https://learningstatisticswithr.com/book/))

I would also suggest starting with a dataset or project that you are
familiar with. Try reproducing something that you have already done in
another program, or one that your advisor or colleague has done and
published on.

## Other useful sources

Using Visual Markdown:
<https://rstudio.github.io/visual-markdown-editing/>

What They Forgot to teach you about R: <https://rstats.wtf/>

`papaja` (Preparting APA Journal Articles):
<https://frederikaust.com/papaja_man/> <https://github.com/crsh/papaja>

About RMarkdown: <https://bookdown.org/yihui/rmarkdown/>

About Quarto: <https://quarto.org/>

I always have the first chunk to set up everything that I need. That way
all packages are right in the beginning and this can be the chunk that
is used to reload and refresh things.

```{r}
library(here) #relative file path
library(rio) #import data
library(easystats) #collection of tools
library(dplyr) #data wrangling
library(ggplot2) #data visualization
library(psych) #cronbach alpha (and a whole lot of other stuff)
library(janitor) #cleaning variable names
library(sjPlot) #making nice tables
library(broom)

#tell R not to display scientific notation
options(scipen=999)

# Create a list of citations of the libraries that are used
report::cite_packages()

# Identify the version of R and the type of machine that is running it
report::report_system()

# Create a list of the package names and the in-text citation 
#report::report_packages()

cah <- import(here("Data", "CAH_Data.csv")) %>% 
  clean_names()
```

Or you can have it set up in text:

`r report::report_packages()`

# Data wrangling

I usually then have a section to setup and work with all the data that I
need. - Reverse Scoring with `datawizard::reverse()` - Computing
variables with `dplyr::mutate()`

## Visualization

Sometimes there will be a data visualization that is needed or even some
tables of descriptives

```{r}
cah %>% 
  describe() %>% 
  select(c(n, mean)) %>% 
  tab_df(title = "Table 1 - Descriptive Statistics")
```

```{r}
cah %>% 
  select(c(age, books, transformers)) %>% 
  tab_corr(title = "Table 2 - Correlations", 
           triangle = "lower", 
           var.labels = c("Age", "# of books", "# of Transformers Movies"))
```

# Statistical Models

Then the next section will be broken up into my research questions

Linear Regression

```{r}

model1 <- lm(income ~ gender + ghosts + transformers, 
             data = cah)

summary(model1)
tab_model(model1, 
          title = "Table 3 - Predicting Income", 
          dv.labels = "Income")

report(model1)

model1$coefficients[7]

transformers_beta <- broom::tidy(model1) %>% 
  filter(term == "transformers") %>% 
  select(estimate)

```

Sample 1 We ran a linear regression model to predict income and found
that the number of transformers movies significantly predicted a lower
income (b = `r round(model1$coefficients[7],2)`)

Sample 2 - Broom We ran a linear regression model to predict income and
found that the number of transformers movies significantly predicted a
lower income (b = `r transformers_beta`)

# Some example code chunks

Cool library to work with missing data! `install.packages('misty')`
<https://www.rdocumentation.org/packages/misty/versions/0.7.1>

### Reverse Scoring

```{r}
#https://easystats.github.io/datawizard/reference/reverse.html

## Reverse Scoring

#cleanr <- reverse(data, 
#                  select = c(BFI_6_T1, BFI_21_T1, BFI_31_T1))
```

### Easily identify Duplicates

```{r}
#https://easystats.github.io/datawizard/reference/data_duplicated.html

## Identify Duplicates

#duplicate_raw <- data_duplicated(raw, select = "id")
```

## Generate Cronbach's Alpha

```{r}
## Cronbach's Alpha
#dat %>% 
#  select(BPAQ_1, BPAQ_2, BPAQ_3, BPAQ_4, BPAQ_5) %>% 
#  psych::alpha()
```
