GLPFinal

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

library(readxl)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(knitr)

dat <- read_excel("GLP1 Survey Data with Groups.xlsx", sheet = "Filtered")
New names:
• `ID Number...65` -> `ID Number...63`
• `Gender...69` -> `Gender...67`
• `Age...70` -> `Age...68`
state_clean <- tolower(trimws(dat$`State They Are From`))
age_raw <- trimws(dat$Age_Group)

# Check: warn if any age labels won't match the factor levels below
age_levels <- c("18-19", "20-21", "22 and older")
bad_ages <- unique(age_raw[!is.na(age_raw) & !(age_raw %in% age_levels)])
if (length(bad_ages) > 0) {
  warning("Unmatched Age_Group values (will become NA): ", paste(bad_ages, collapse = ", "))
}

dat <- dat %>%
  mutate(
    # People who quit before the definition question are coded missing rather than 0.
    # To count them as 0 instead, change NA_real_ below to 0.
    Score = if_else(
      toupper(trimws(Defintion)) == "DID NOT ANSWER",
      NA_real_,
      as.numeric(`Final Score`)
    ),
    State = case_when(
      state_clean %in% c("mn", "minnesota", "minnestoa") ~ "Minnesota",
      state_clean %in% c("wi", "wisconsin", "wisconson", "rice lake, wi") ~ "Wisconsin",
      state_clean == "iowa" ~ "Iowa"
    ),
    Age_Group = factor(age_raw, levels = age_levels),
    State = factor(State, levels = c("Minnesota", "Wisconsin", "Iowa"))
  ) %>%
  filter(!is.na(State))   # Keep only Minnesota, Wisconsin, and Iowa

# How many people in this data did not answer the definition question
n_no_answer <- sum(toupper(trimws(dat$Defintion)) == "DID NOT ANSWER", na.rm = TRUE)
cat("Did not answer the definition question:", n_no_answer, "\n")
Did not answer the definition question: 63 
# Counts and percentages for one variable
freq_table <- function(x) {
  counts <- table(x, useNA = "ifany")
  data.frame(
    Group = ifelse(is.na(names(counts)), "Missing", names(counts)),
    N = as.vector(counts),
    Percent = round(100 * as.vector(counts) / sum(counts), 1)
  )
}

# Mean, SD, N, and one-way ANOVA p-value
compare_score <- function(data, group) {
  d <- data %>% filter(!is.na(Score), !is.na(.data[[group]]))
  d$Group <- droplevels(factor(d[[group]]))
  p <- anova(lm(Score ~ Group, data = d))$`Pr(>F)`[1]
  
  d %>%
    group_by(Group) %>%
    summarise(Mean = round(mean(Score), 2), SD = round(sd(Score), 2), N = n(), .groups = "drop") %>%
    mutate(Pval = c(format.pval(p, digits = 3, eps = 0.001), rep("", n() - 1)))
}

# 1. College distribution
kable(arrange(freq_table(dat$College), desc(N)), caption = "College Distribution")
College Distribution
Group N Percent
Nursing & Health Sciences 281 52.0
Science & Engineering 112 20.7
Education 52 9.6
Liberal Arts 50 9.3
Business 40 7.4
Undeclared 5 0.9
# 2. Definition score distribution
dat %>%
  summarise(
    N = sum(!is.na(Score)),
    Missing = sum(is.na(Score)),
    Mean = round(mean(Score, na.rm = TRUE), 2),
    SD = round(sd(Score, na.rm = TRUE), 2),
    Median = median(Score, na.rm = TRUE),
    Q1 = quantile(Score, 0.25, na.rm = TRUE),
    Q3 = quantile(Score, 0.75, na.rm = TRUE),
    Min = min(Score, na.rm = TRUE),
    Max = max(Score, na.rm = TRUE)
  ) %>%
  kable(caption = "Definition Score Summary")
Definition Score Summary
N Missing Mean SD Median Q1 Q3 Min Max
477 63 2.47 1.26 3 2 3 0 6
kable(freq_table(dat$Score), caption = "Definition Score Distribution")
Definition Score Distribution
Group N Percent
0 53 9.8
1 22 4.1
2 158 29.3
3 166 30.7
4 54 10.0
5 20 3.7
6 4 0.7
Missing 63 11.7
# 3. Q6 distribution
kable(arrange(freq_table(dat$State), desc(N)), caption = "Q6: State Distribution")
Q6: State Distribution
Group N Percent
Minnesota 390 72.2
Wisconsin 140 25.9
Iowa 10 1.9
# 4. Definition score by age group
kable(compare_score(dat, "Age_Group"), caption = "Definition Score by Age Group")
Definition Score by Age Group
Group Mean SD N Pval
18-19 2.29 1.36 190 0.0386
20-21 2.55 1.16 227
22 and older 2.70 1.25 60
# 5. Definition score by state
kable(compare_score(dat, "State"), caption = "Definition Score by State")
Definition Score by State
Group Mean SD N Pval
Minnesota 2.44 1.31 347 0.786
Wisconsin 2.53 1.11 121
Iowa 2.56 1.33 9
# Pairwise comparisons between states (Tukey HSD)
tukey <- TukeyHSD(aov(Score ~ State, data = dat))$State

data.frame(
  Comparison = rownames(tukey),
  Difference = round(tukey[, "diff"], 2),
  Pval = format.pval(tukey[, "p adj"], digits = 3, eps = 0.001),
  row.names = NULL
) %>%
  kable(caption = "Pairwise State Comparisons")
Pairwise State Comparisons
Comparison Difference Pval
Wisconsin-Minnesota 0.09 0.787
Iowa-Minnesota 0.11 0.961
Iowa-Wisconsin 0.03 0.998