Replicate original tables
Before doing fancier analysis and visualizations, I replicated the three summary tables that were originally created with SPSS.
Table 1: Summary statistics
Original table
Table 1: Summary statistics
Table using current data
psm_indexes_long <- psm %>%
select (ID, starts_with ("index" )) %>%
gather (index, value, - ID) %>%
filter (! str_detect (index, "_z" )) %>%
mutate (index = fct_inorder (index, ordered = TRUE ),
index = fct_recode (index,
Perry = "index_perry" ,
MSPB5 = "index_msp" ,
Grant = "index_grant" ,
International = "index_intl" ))
psm_indexes_long %>%
filter (! is.na (value)) %>%
group_by (index) %>%
summarize (Observations = n (),
Mean = mean (value),
` Std. Dev ` = sd (value),
Minimum = min (value),
Maximum = max (value)) %>%
rename (Index = index) %>%
pandoc.table (justify = "lccccc" )
Perry
291
77.31
12.28
37
111
MSPB5
365
17.55
3.122
5
25
Grant
418
17.11
2.65
4
20
International
319
62.77
8.302
33
80
Uh oh, this doesn’t match. 🤔
The mean, min, and max are all off, but oddly enough, the N and standard deviations are both correct.
Table 2: High PSM
Original table
Table 2: Proportions of Students with “High” Levels of PSM
Table using current data
This is slightly tricky (and wrong) because the ranges of possible values don’t match what’s in the original Word file. The Perry index, for example, should range from 0–96, but in reality it goes up to a maximum of 111 (and I don’t know what the actual real life maximum should be, given what it’s calculating). The same issue appears in the other indexes. Grant should range from 0–16, but here it ranges from 0–20, so the top quartile is 15–20 instead of 13–16.
So for now this is wrong, but the code exists and everything can be made right once we figure out the issues with index calculation.
possible_quantiles <- psm_indexes_long %>%
distinct (index) %>%
mutate (possible_min = c (0 , 0 , 0 , 0 ),
possible_max = c (116 , 25 , 20 , 80 )) %>%
nest (- index) %>%
mutate (quantiles = data %>% map (~ quantile (.$ possible_min: .$ possible_max))) %>%
unnest (quantiles %>% map (tidy))
possible_quantiles_wide <- possible_quantiles %>%
mutate (names = fct_inorder (names)) %>%
spread (names, x) %>%
mutate (` Top quartile range ` = paste0 (` 75% ` , "-" , ` 100% ` ),
` Possible range (wrong?) ` = paste0 (` 0% ` , "-" , ` 100% ` ))
psm_index_quantiles <- psm_indexes_long %>%
filter (! is.na (value)) %>%
nest (- index) %>%
mutate (temp = map2 (
.x = data, .y = index,
~ mutate (.x, quartile = cut (
value,
breaks = filter (possible_quantiles, index == .y) %>% pull (x),
labels = FALSE , right = FALSE , include.lowest = TRUE
))
)) %>%
unnest (temp)
psm_index_quantiles %>%
group_by (index, quartile) %>%
summarize (n = n ()) %>%
mutate (perc = n / sum (n)) %>%
ungroup () %>%
filter (quartile == 4 ) %>%
left_join (possible_quantiles_wide, by = "index" ) %>%
mutate (perc = scales:: percent (perc)) %>%
select (Index = index, N = n, ` Possible range (wrong?) ` ,
` Top quartile range ` , ` Students in top quartile ` = perc) %>%
pandoc.table (justify = "lcccc" , split.table = Inf )
Perry
59
0-116
87-116
20.3%
MSPB5
142
0-25
18.75-25
38.9%
Grant
355
0-20
15-20
84.9%
International
218
0-80
60-80
68.3%
Table 3: PSM correlations
Original table
Table 3: Correlation Matrix for PSM Measures
Table using current data
psm_cor <- psm_indexes_long %>%
spread (index, value) %>%
select (- ID) %>%
cor (use = "na.or.complete" )
psm_cor[upper.tri (psm_cor)] <- NA
psm_cor_long <- psm_cor %>%
as.data.frame () %>%
rownames_to_column () %>%
as.tibble () %>%
gather (var2, value, - rowname, na.rm = TRUE ) %>%
mutate (rowname = fct_inorder (rowname, ordered = TRUE ),
var2 = factor (var2, levels = levels (rowname), ordered= TRUE ))
ggplot (psm_cor_long, aes (x = fct_rev (rowname), y = fct_rev (var2), fill = value)) +
geom_tile () +
geom_text (aes (label = round (value, 2 )),
family = "Roboto Condensed" , fontface = "plain" ) +
scale_fill_gradient (low = "white" , high = "#eb6864" ,
limit = c (floor (min (psm_cor_long$ value) * 10 ) / 10 , 0.999 ),
na.value = "grey95" ) +
labs (x = NULL , y = NULL ) +
guides (fill = FALSE ) +
coord_equal () +
theme_psm () +
theme (panel.grid.major = element_blank ())
LS0tCnRpdGxlOiAiU3VtbWFyeSBzdGF0aXN0aWNzIgpkYXRlOiAiMjAxNy0xMi0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l0aCgiaW5kZXgiKSkgJT4lCiAgZ2F0aGVyKGluZGV4LCB2YWx1ZSwgLUlEKSAlPiUKICBmaWx0ZXIoIXN0cl9kZXRlY3QoaW5kZXgsICJfeiIpKSAlPiUKICBtdXRhdGUoaW5kZXggPSBmY3RfaW5vcmRlcihpbmRleCwgb3JkZXJlZCA9IFRSVUUpLAogICAgICAgICBpbmRleCA9IGZjdF9yZWNvZGUoaW5kZXgsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBQZXJyeSA9ICJpbmRleF9wZXJyeSIsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBNU1BCNSA9ICJpbmRleF9tc3AiLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgR3JhbnQgPSAiaW5kZXhfZ3JhbnQiLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgSW50ZXJuYXRpb25hbCA9ICJpbmRleF9pbnRsIikpCgpwc21faW5kZXhlc19sb25nICU+JQogIGZpbHRlcighaXMubmEodmFsdWUpKSAlPiUKICBncm91cF9ieShpbmRleCkgJT4lCiAgc3VtbWFyaXplKE9ic2VydmF0aW9ucyA9IG4oKSwKICAgICAgICAgICAgTWVhbiA9IG1lYW4odmFsdWUpLAogICAgICAgICAgICBgU3RkLiBEZXZgID0gc2QodmFsdWUpLAogICAgICAgICAgICBNaW5pbXVtID0gbWluKHZhbHVlKSwKICAgICAgICAgICAgTWF4aW11bSA9IG1heCh2YWx1ZSkpICU+JQogIHJlbmFtZShJbmRleCA9IGluZGV4KSAlPiUKICBwYW5kb2MudGFibGUoanVzdGlmeSA9ICJsY2NjY2MiKQpgYGAKCipVaCBvaCwgdGhpcyBkb2Vzbid0IG1hdGNoLiogYHIgZW1vOjpqaSgidGhpbmtpbmciKWAgCgpUaGUgbWVhbiwgbWluLCBhbmQgbWF4IGFyZSBhbGwgb2ZmLCBidXQgb2RkbHkgZW5vdWdoLCB0aGUgTiBhbmQgc3RhbmRhcmQgZGV2aWF0aW9ucyBhcmUgYm90aCBjb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