```{r setup, include = FALSE}
if(!"corx" %in% rownames(installed.packages())) install.packages("corx")

library(papaja)
library(tidyverse)
library(corx)
```

```{r analysis-preferences}
# Seed for random number generation
set.seed(42)
knitr::opts_chunk$set(cache.extra = knitr::rand_seed)
```

We could start our introduction here, potentially saying something about the history of the Big Five [@goldberg1990alternative], or about Roberts and colleagues' [-@roberts2005]. We could also mention @mcclelland1993statistical.

# Methods
We report how we determined our sample size, all data exclusions (if any), all manipulations, and all measures in the study. <!-- 21-word solution (Simmons, Nelson & Simonsohn, 2012; retrieved from http://ssrn.com/abstract=2160588) -->

## Participants

We have `r nrow(psych::bfi)` participants who completed some or all of the Big Five.

## Material

## Procedure

## Data analysis
We used `r cite_r("r-references.bib")` for all our analyses.


# Results

```{r descrips-tab, results = "asis" }
b5 <- psych::bfi %>% 
  # score items with psych's scoreVeryFast
  cbind( # column-bind the 5 scale scores
    # using scoreVeryFast from psych
    # which scores questionnaires based on a 
    # key
    psych::scoreVeryFast(psych::bfi.keys, # scoring key goes here, which psych has for this data
                       psych::bfi) # data goes here
  )

descriptives <- b5 %>%
  select(agree:openness) %>% 
  gather(scale, score) %>% 
  group_by(scale) %>% 
  summarize(
    Mean = mean(score)
    , Median = median(score)
    , SD = sd(score)
    , Min = min(score)
    , Max = max(score)
  )

apa_table(
  descriptives
  , caption = "Descriptive statistics of Big Five Scale Scores."
  , note = "This table was created with apa_table()."
  , escape = TRUE
)
```

```{r scale-intercors}
library(corx)

cor <-  b5 %>%
  select(agree:openness) %>% 
  corx(triangle = "lower",
       stars = c(0.05, 0.01, 0.001))
```

```{r scale-intercors-tbl, results = "asis"}
papaja::apa_table(cor$apa, # apa contains the data.frame needed for apa_table
                  caption = "Example corr matrix",
                  note = "* p < 0.05; ** p < 0.01; *** p < 0.001")
```

```{r regressions}
b5 <- b5 %>% 
  mutate(
    # re-labelling education
    education = case_when(
      education == 1 ~ "Some HS",
      education == 2 ~ " (Finished HS)",
      education == 3 ~ " (Some College)",
      education == 4 ~ " (College Grad)",
      education == 5 ~ " (Grad Degree)"
    ),
    # turning it into a factor, and putting levels
    # in order that I'd like them in 
    # (this makes some HS the reference group).
    education = fct_relevel(education, 
                            "Some HS",
                            " (Finished HS)",
                            " (Some College)",
                            " (College Grad)",
                            " (Grad Degree)"),
    age = age - mean(age, na.rm = TRUE),
  ) %>%
  # removing NAs to make thing a little
  # easier for today
  filter(!is.na(conscientious),
         !is.na(age),
         !is.na(education))

m1 <- lm(conscientious ~ age, data = b5)
m2 <- lm(conscientious ~ age + education, data = b5)
# model 1 results
m1_results <- apa_print(m1)
# model 2 results
m2_results <- apa_print(m2)
# model comparison results
mod_comp_results <- apa_print(list(m1, m2),
                              # setting to 0
                              # otherwise it creates bootstrapped
                              # CI for delta R^2 - we will cover bootstrapping 
                              # soon.
                              boot_samples = 0)
```

```{r reg-models-tbl, results = "asis"}
mod_comp_results$table %>% 
  apa_table(caption = "Regressing Conscientiousness on Age and Education",
            note = "Model comparison compares a model with just age to a model that has age and education.")
```

(ref:fig-ageXconsc-cap) Conscientiousness by Age.

```{r fig-ageXconsc, fig.cap="(ref:fig-ageXconsc-cap)"}
ggplot(b5, aes(x = age, y = conscientious)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(x = "Age",
       y = "Conscientiousness") +
  # theme_apa puts it in apa format
  theme_apa(box = TRUE)
```

We calculated scale scores for each of the Big Five. Descriptive statistics for each Big Five scale are shown in Table\ \@ref(tab:descrips-tab), where it is apparent that means were near the scale midpoints of 4.5, with the exception of neuroticism which had a much lower mean of `r printnum(mean(b5$neuroticism, na.rm = TRUE))`.

Scale inter-correlations can be found in Table\ \@ref(tab:scale-intercors-tbl), where one can see that the smallest correlation was between neuroticism and opennes (*r* = `r printnum(cor$r["neuroticism","openness"])`) and the largest correlation was between extraversion and agreeableness (*r* = `r printnum(cor$r["agree","extraversion"])`).

We next examined the extent to which grand-mean centered age and education are related to conscientiousness, based on the postulates of social investment theory [@roberts2005]. The results from a model with just age and a model with age and education are shown in Table\ \@ref(tab:reg-models-tbl) below. Age had a small but significant, positive association with conscientiousness, both when education is (`r m2_results$full_result$age`) and is not (`r m1_results$full_result$age`) included as a covariate. Education did significantly but modestly improve the model (`r mod_comp_results$full_result`). Interestingly, the pattern of results did not follow a monotonic increase from some highschool to graduate degree. Indeed, the only significant difference in conscientiousness across education was between participants who didn't finish high school and participants who reported having some college (`r m2_results$full_result$education_Some_College`). At the sample average age, participants who didn't finish highschool had an average conscientiousness score of ($M_{SomeHS}$ = `r printnum(m2$coefficients[["(Intercept)"]])`) while participants with some college had an average conscientiousness score of ($M_{SomeCollege}$ = `r printnum(m2$coefficients[["(Intercept)"]] + m2$coefficients[["education (Some College)"]])`). The remaining effects of education were small and non-significant (see Table\ \@ref(tab:reg-models-tbl)). 

Figure\ \@ref(fig:fig-ageXconsc) depicts the small, linear increase of conscientiousness across the age range of our participants. 

# Discussion


\newpage

# References
```{r create_r-references}
r_refs(file = "r-references.bib")
```

\begingroup
\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}

<div id = "refs"></div>
\endgroup
