install.packages(c( “readxl”, # Excel import “dplyr”, # data manipulation “tidyr”, # reshaping “ggplot2”, # plotting “knitr”, # tables / Rmd “kableExtra”,# nicer tables “janitor” # clean_names(), tabyl() ))
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(ggplot2)
df <- read_excel("DM.xlsx")
## New names:
## • `PS "In the past 12 months, were you ever worried your food would run out
## before you had money to buy more?"` -> `PS "In the past 12 months, were you
## ever worried your food would run out before you had money to buy more?"...23`
## • `PS "In the past 12 months, did the food you could afford not last until the
## end of the month, and you couldn’t get more?"` -> `PS "In the past 12 months,
## did the food you could afford not last until the end of the month, and you
## couldn’t get more?"...24`
## • `PS "In the last month, did anyone in your household have to skip meals due
## to lack of food?"` -> `PS "In the last month, did anyone in your household
## have to skip meals due to lack of food?"...25`
## • `T0- In the past week, how often did you check your blood sugar?` -> `T0- In
## the past week, how often did you check your blood sugar?...30`
## • `T0- Are you Confident in making food choices that help control your blood
## sugar?` -> `T0- Are you Confident in making food choices that help control
## your blood sugar?...31`
## • `T0- Are you confident in understanding how to read food labels and nutrition
## information?` -> `T0- Are you confident in understanding how to read food
## labels and nutrition information?...32`
## • `T0- Were your sugar levels usually in your target range last week?` -> `T0-
## Were your sugar levels usually in your target range last week?...33`
## • `T0- In the past week, did you miss any doses of you diabetes medications?`
## -> `T0- In the past week, did you miss any doses of you diabetes
## medications?...34`
## • `T1- Did the Magnet on Portion control impact your plate or portion sizes` ->
## `T1- Did the Magnet on Portion control impact your plate or portion
## sizes...36`
## • `T1- In the past week, how many meals did you make using food from the
## program?` -> `T1- In the past week, how many meals did you make using food
## from the program?...38`
## • `T1- In the past week, did you find it difficult to use the food in your
## meals?` -> `T1- In the past week, did you find it difficult to use the food
## in your meals?...39`
## • `T1- In the past week, how often did you check your blood sugar?` -> `T1- In
## the past week, how often did you check your blood sugar?...40`
## • `T1- Were your sugar levels usually in your target range last week?` -> `T1-
## Were your sugar levels usually in your target range last week?...41`
## • `T1- Are you Confident in making food choices that help control your blood
## sugar?` -> `T1- Are you Confident in making food choices that help control
## your blood sugar?...42`
## • `T1- Are you confident in understanding how to read food labels and nutrition
## information?` -> `T1- Are you confident in understanding how to read food
## labels and nutrition information?...43`
## • `T1- In the past week, did you miss any doses of you diabetes medications?`
## -> `T1- In the past week, did you miss any doses of you diabetes
## medications?...44`
## • `T1- Since our last check in, have you had an appointment with a healthcare
## provider for your diabetes?` -> `T1- Since our last check in, have you had an
## appointment with a healthcare provider for your diabetes?...45`
## • `T1- Did the Magnet on Portion control impact your plate or portion sizes` ->
## `T1- Did the Magnet on Portion control impact your plate or portion
## sizes...46`
## • `T2- In the past week, how many meals did you make using food from the
## program?` -> `T2- In the past week, how many meals did you make using food
## from the program?...48`
## • `T2- In the past week, did you find it difficult to use the food in your
## meals?` -> `T2- In the past week, did you find it difficult to use the food
## in your meals?...49`
## • `T2- In the past week, how often did you check your blood sugar?` -> `T2- In
## the past week, how often did you check your blood sugar?...50`
## • `T2- Were your sugar levels usually in your target range last week?` -> `T2-
## Were your sugar levels usually in your target range last week?...51`
## • `T2- Are you Confident in making food choices that help control your blood
## sugar?` -> `T2- Are you Confident in making food choices that help control
## your blood sugar?...52`
## • `T2- Are you confident in understanding how to read food labels and nutrition
## information?` -> `T2- Are you confident in understanding how to read food
## labels and nutrition information?...53`
## • `T2- In the past week, did you miss any doses of you diabetes medications?`
## -> `T2- In the past week, did you miss any doses of you diabetes
## medications?...54`
## • `T2- Since our last check in, have you had an appointment with a healthcare
## provider for your diabetes?` -> `T2- Since our last check in, have you had an
## appointment with a healthcare provider for your diabetes?...55`
## • `T2- Did you throw away any of the food from the deliveries?` -> `T2- Did you
## throw away any of the food from the deliveries?...56`
## • `T2- How much did this program help you feel more in control of your
## diabetes?` -> `T2- How much did this program help you feel more in control of
## your diabetes?...57`
## • `T2- Do you have suggestions or feedback to improve this program for others?`
## -> `T2- Do you have suggestions or feedback to improve this program for
## others?...58`
## • `` -> `...98`
## • `` -> `...99`
## • `` -> `...100`
## • `` -> `...101`
## • `` -> `...102`
## • `` -> `...103`
## • `` -> `...104`
## • `` -> `...105`
## • `` -> `...106`
## • `` -> `...107`
## • `` -> `...108`
## • `` -> `...109`
## • `` -> `...110`
## • `` -> `...111`
## • `` -> `...112`
## • `` -> `...113`
## • `` -> `...114`
## • `` -> `...119`
## • `PS "In the past 12 months, were you ever worried your food would run out
## before you had money to buy more?"` -> `PS "In the past 12 months, were you
## ever worried your food would run out before you had money to buy
## more?"...120`
## • `PS "In the past 12 months, did the food you could afford not last until the
## end of the month, and you couldn’t get more?"` -> `PS "In the past 12 months,
## did the food you could afford not last until the end of the month, and you
## couldn’t get more?"...121`
## • `PS "In the last month, did anyone in your household have to skip meals due
## to lack of food?"` -> `PS "In the last month, did anyone in your household
## have to skip meals due to lack of food?"...122`
## • `` -> `...123`
## • `` -> `...124`
## • `` -> `...125`
## • `` -> `...126`
## • `` -> `...127`
## • `T0- In the past week, how often did you check your blood sugar?` -> `T0- In
## the past week, how often did you check your blood sugar?...128`
## • `T1- In the past week, how often did you check your blood sugar?` -> `T1- In
## the past week, how often did you check your blood sugar?...129`
## • `T2- In the past week, how often did you check your blood sugar?` -> `T2- In
## the past week, how often did you check your blood sugar?...130`
## • `T0- In the past week, how often did you check your blood sugar?` -> `T0- In
## the past week, how often did you check your blood sugar?...131`
## • `T1- In the past week, how often did you check your blood sugar?` -> `T1- In
## the past week, how often did you check your blood sugar?...132`
## • `T2- In the past week, how often did you check your blood sugar?` -> `T2- In
## the past week, how often did you check your blood sugar?...133`
## • `T0- Were your sugar levels usually in your target range last week?` -> `T0-
## Were your sugar levels usually in your target range last week?...134`
## • `T1- Were your sugar levels usually in your target range last week?` -> `T1-
## Were your sugar levels usually in your target range last week?...135`
## • `T2- Were your sugar levels usually in your target range last week?` -> `T2-
## Were your sugar levels usually in your target range last week?...136`
## • `T0- Were your sugar levels usually in your target range last week?` -> `T0-
## Were your sugar levels usually in your target range last week?...137`
## • `T1- Were your sugar levels usually in your target range last week?` -> `T1-
## Were your sugar levels usually in your target range last week?...138`
## • `T2- Were your sugar levels usually in your target range last week?` -> `T2-
## Were your sugar levels usually in your target range last week?...139`
## • `T0- In the past week, did you miss any doses of you diabetes medications?`
## -> `T0- In the past week, did you miss any doses of you diabetes
## medications?...140`
## • `T1- In the past week, did you miss any doses of you diabetes medications?`
## -> `T1- In the past week, did you miss any doses of you diabetes
## medications?...141`
## • `T2- In the past week, did you miss any doses of you diabetes medications?`
## -> `T2- In the past week, did you miss any doses of you diabetes
## medications?...142`
## • `T0- In the past week, did you miss any doses of you diabetes medications?`
## -> `T0- In the past week, did you miss any doses of you diabetes
## medications?...143`
## • `T1- In the past week, did you miss any doses of you diabetes medications?`
## -> `T1- In the past week, did you miss any doses of you diabetes
## medications?...144`
## • `T2- In the past week, did you miss any doses of you diabetes medications?`
## -> `T2- In the past week, did you miss any doses of you diabetes
## medications?...145`
## • `T0- Are you Confident in making food choices that help control your blood
## sugar?` -> `T0- Are you Confident in making food choices that help control
## your blood sugar?...146`
## • `T1- Are you Confident in making food choices that help control your blood
## sugar?` -> `T1- Are you Confident in making food choices that help control
## your blood sugar?...147`
## • `T2- Are you Confident in making food choices that help control your blood
## sugar?` -> `T2- Are you Confident in making food choices that help control
## your blood sugar?...148`
## • `T0- Are you Confident in making food choices that help control your blood
## sugar?` -> `T0- Are you Confident in making food choices that help control
## your blood sugar?...149`
## • `T1- Are you Confident in making food choices that help control your blood
## sugar?` -> `T1- Are you Confident in making food choices that help control
## your blood sugar?...150`
## • `T2- Are you Confident in making food choices that help control your blood
## sugar?` -> `T2- Are you Confident in making food choices that help control
## your blood sugar?...151`
## • `T0- Are you confident in understanding how to read food labels and nutrition
## information?` -> `T0- Are you confident in understanding how to read food
## labels and nutrition information?...152`
## • `T1- Are you confident in understanding how to read food labels and nutrition
## information?` -> `T1- Are you confident in understanding how to read food
## labels and nutrition information?...153`
## • `T2- Are you confident in understanding how to read food labels and nutrition
## information?` -> `T2- Are you confident in understanding how to read food
## labels and nutrition information?...154`
## • `T0- Are you confident in understanding how to read food labels and nutrition
## information?` -> `T0- Are you confident in understanding how to read food
## labels and nutrition information?...155`
## • `T1- Are you confident in understanding how to read food labels and nutrition
## information?` -> `T1- Are you confident in understanding how to read food
## labels and nutrition information?...156`
## • `T2- Are you confident in understanding how to read food labels and nutrition
## information?` -> `T2- Are you confident in understanding how to read food
## labels and nutrition information?...157`
## • `T1- In the past week, how many meals did you make using food from the
## program?` -> `T1- In the past week, how many meals did you make using food
## from the program?...158`
## • `T2- In the past week, how many meals did you make using food from the
## program?` -> `T2- In the past week, how many meals did you make using food
## from the program?...159`
## • `T1- In the past week, did you find it difficult to use the food in your
## meals?` -> `T1- In the past week, did you find it difficult to use the food
## in your meals?...160`
## • `T2- In the past week, did you find it difficult to use the food in your
## meals?` -> `T2- In the past week, did you find it difficult to use the food
## in your meals?...161`
## • `T1- In the past week, did you find it difficult to use the food in your
## meals?` -> `T1- In the past week, did you find it difficult to use the food
## in your meals?...162`
## • `T2- In the past week, did you find it difficult to use the food in your
## meals?` -> `T2- In the past week, did you find it difficult to use the food
## in your meals?...163`
## • `T2- Did you throw away any of the food from the deliveries?` -> `T2- Did you
## throw away any of the food from the deliveries?...164`
## • `T2- Did you throw away any of the food from the deliveries?` -> `T2- Did you
## throw away any of the food from the deliveries?...165`
## • `T1- Did the Magnet on Portion control impact your plate or portion sizes` ->
## `T1- Did the Magnet on Portion control impact your plate or portion
## sizes...166`
## • `T1- Since our last check in, have you had an appointment with a healthcare
## provider for your diabetes?` -> `T1- Since our last check in, have you had an
## appointment with a healthcare provider for your diabetes?...167`
## • `T2- Since our last check in, have you had an appointment with a healthcare
## provider for your diabetes?` -> `T2- Since our last check in, have you had an
## appointment with a healthcare provider for your diabetes?...168`
## • `T1- Since our last check in, have you had an appointment with a healthcare
## provider for your diabetes?` -> `T1- Since our last check in, have you had an
## appointment with a healthcare provider for your diabetes?...169`
## • `T2- Since our last check in, have you had an appointment with a healthcare
## provider for your diabetes?` -> `T2- Since our last check in, have you had an
## appointment with a healthcare provider for your diabetes?...170`
## • `T1- Did the Magnet on Portion control impact your plate or portion sizes` ->
## `T1- Did the Magnet on Portion control impact your plate or portion
## sizes...177`
## • `T2- How much did this program help you feel more in control of your
## diabetes?` -> `T2- How much did this program help you feel more in control of
## your diabetes?...178`
## • `T2- Do you have suggestions or feedback to improve this program for others?`
## -> `T2- Do you have suggestions or feedback to improve this program for
## others?...179`
## • `T2- Did you throw away any of the food from the deliveries?` -> `T2- Did you
## throw away any of the food from the deliveries?...180`
# Quick checks
dim(df) # rows and columns
## [1] 67 180
names(df) # column names
## [1] "Pre Enrollment Notes"
## [2] "Age"
## [3] "PreferredLanguage"
## [4] "Disch Date/Time"
## [5] "CC"
## [6] "Admission Diagnosis"
## [7] "Admitting Provider"
## [8] "Discharge Disposition"
## [9] "LACE+ READMISSION SCORE Score Column"
## [10] "Unplanned Readmission Score"
## [11] "Primary Cvg"
## [12] "HbA1c Value (Last 3 Months)"
## [13] "Pt. Portal Status"
## [14] "Hosp Last 365 Days"
## [15] "ED Vis Last 90 Days"
## [16] "Hosp Last 90 Days"
## [17] "PCP"
## [18] "Race"
## [19] "Ethnicity"
## [20] "Legal Sex"
## [21] "Zip code"
## [22] "Prescreen Contacted Y/N"
## [23] "PS \"In the past 12 months, were you ever worried your food would run out before you had money to buy more?\"...23"
## [24] "PS \"In the past 12 months, did the food you could afford not last until the end of the month, and you couldn’t get more?\"...24"
## [25] "PS \"In the last month, did anyone in your household have to skip meals due to lack of food?\"...25"
## [26] "(Pre Screen) Interested in Program?"
## [27] "T0 Contact"
## [28] "(Screen for Enrollment) Interested in Program"
## [29] "T0- Are you likely to eat the food as part of your normal daily routine?"
## [30] "T0- In the past week, how often did you check your blood sugar?...30"
## [31] "T0- Are you Confident in making food choices that help control your blood sugar?...31"
## [32] "T0- Are you confident in understanding how to read food labels and nutrition information?...32"
## [33] "T0- Were your sugar levels usually in your target range last week?...33"
## [34] "T0- In the past week, did you miss any doses of you diabetes medications?...34"
## [35] "T0- Since your discharge from the hospital, have you had an appointment with a healthcare provider for your diabetes?"
## [36] "T1- Did the Magnet on Portion control impact your plate or portion sizes...36"
## [37] "T1 Contact (Y/N)"
## [38] "T1- In the past week, how many meals did you make using food from the program?...38"
## [39] "T1- In the past week, did you find it difficult to use the food in your meals?...39"
## [40] "T1- In the past week, how often did you check your blood sugar?...40"
## [41] "T1- Were your sugar levels usually in your target range last week?...41"
## [42] "T1- Are you Confident in making food choices that help control your blood sugar?...42"
## [43] "T1- Are you confident in understanding how to read food labels and nutrition information?...43"
## [44] "T1- In the past week, did you miss any doses of you diabetes medications?...44"
## [45] "T1- Since our last check in, have you had an appointment with a healthcare provider for your diabetes?...45"
## [46] "T1- Did the Magnet on Portion control impact your plate or portion sizes...46"
## [47] "T2 Contact (Y/N)"
## [48] "T2- In the past week, how many meals did you make using food from the program?...48"
## [49] "T2- In the past week, did you find it difficult to use the food in your meals?...49"
## [50] "T2- In the past week, how often did you check your blood sugar?...50"
## [51] "T2- Were your sugar levels usually in your target range last week?...51"
## [52] "T2- Are you Confident in making food choices that help control your blood sugar?...52"
## [53] "T2- Are you confident in understanding how to read food labels and nutrition information?...53"
## [54] "T2- In the past week, did you miss any doses of you diabetes medications?...54"
## [55] "T2- Since our last check in, have you had an appointment with a healthcare provider for your diabetes?...55"
## [56] "T2- Did you throw away any of the food from the deliveries?...56"
## [57] "T2- How much did this program help you feel more in control of your diabetes?...57"
## [58] "T2- Do you have suggestions or feedback to improve this program for others?...58"
## [59] "T2 \"In the past 12 months, were you ever worried your food would run out before you had money to buy more?\""
## [60] "T2 \"In the past 12 months, did the food you could afford not last until the end of the month, and you couldn’t get more?\""
## [61] "T2 \"In the last month, did anyone in your household have to skip meals due to lack of food?\""
## [62] "Enrolled"
## [63] "Cohort"
## [64] "Delivery 1"
## [65] "D1- Comments"
## [66] "Delivery 2"
## [67] "02- Comments"
## [68] "Delivery 3"
## [69] "03- Comments"
## [70] "Delivery 4"
## [71] "04- Comments"
## [72] "Delivery 5"
## [73] "05- Comments"
## [74] "Delivery 6"
## [75] "06- Comments"
## [76] "Delivery 7"
## [77] "07- Comments"
## [78] "Delivery 8"
## [79] "08- Comments"
## [80] "Delivery 9"
## [81] "09- Comments"
## [82] "Delivery 10"
## [83] "10- Comments"
## [84] "Delivery 11"
## [85] "11- Comments"
## [86] "Delivery 12"
## [87] "12- Comments"
## [88] "Delivery 13"
## [89] "13- Comments"
## [90] "Delivery 14"
## [91] "14- Comments"
## [92] "Delivery 15"
## [93] "15- Comments"
## [94] "Delivery 16"
## [95] "16- Comments"
## [96] "Delivery 17"
## [97] "17- Comments"
## [98] "...98"
## [99] "...99"
## [100] "...100"
## [101] "...101"
## [102] "...102"
## [103] "...103"
## [104] "...104"
## [105] "...105"
## [106] "...106"
## [107] "...107"
## [108] "...108"
## [109] "...109"
## [110] "...110"
## [111] "...111"
## [112] "...112"
## [113] "...113"
## [114] "...114"
## [115] "Percent English Speakers"
## [116] "Percent Spanish Speakers"
## [117] "Percent Male"
## [118] "Percent Female"
## [119] "...119"
## [120] "PS \"In the past 12 months, were you ever worried your food would run out before you had money to buy more?\"...120"
## [121] "PS \"In the past 12 months, did the food you could afford not last until the end of the month, and you couldn’t get more?\"...121"
## [122] "PS \"In the last month, did anyone in your household have to skip meals due to lack of food?\"...122"
## [123] "...123"
## [124] "...124"
## [125] "...125"
## [126] "...126"
## [127] "...127"
## [128] "T0- In the past week, how often did you check your blood sugar?...128"
## [129] "T1- In the past week, how often did you check your blood sugar?...129"
## [130] "T2- In the past week, how often did you check your blood sugar?...130"
## [131] "T0- In the past week, how often did you check your blood sugar?...131"
## [132] "T1- In the past week, how often did you check your blood sugar?...132"
## [133] "T2- In the past week, how often did you check your blood sugar?...133"
## [134] "T0- Were your sugar levels usually in your target range last week?...134"
## [135] "T1- Were your sugar levels usually in your target range last week?...135"
## [136] "T2- Were your sugar levels usually in your target range last week?...136"
## [137] "T0- Were your sugar levels usually in your target range last week?...137"
## [138] "T1- Were your sugar levels usually in your target range last week?...138"
## [139] "T2- Were your sugar levels usually in your target range last week?...139"
## [140] "T0- In the past week, did you miss any doses of you diabetes medications?...140"
## [141] "T1- In the past week, did you miss any doses of you diabetes medications?...141"
## [142] "T2- In the past week, did you miss any doses of you diabetes medications?...142"
## [143] "T0- In the past week, did you miss any doses of you diabetes medications?...143"
## [144] "T1- In the past week, did you miss any doses of you diabetes medications?...144"
## [145] "T2- In the past week, did you miss any doses of you diabetes medications?...145"
## [146] "T0- Are you Confident in making food choices that help control your blood sugar?...146"
## [147] "T1- Are you Confident in making food choices that help control your blood sugar?...147"
## [148] "T2- Are you Confident in making food choices that help control your blood sugar?...148"
## [149] "T0- Are you Confident in making food choices that help control your blood sugar?...149"
## [150] "T1- Are you Confident in making food choices that help control your blood sugar?...150"
## [151] "T2- Are you Confident in making food choices that help control your blood sugar?...151"
## [152] "T0- Are you confident in understanding how to read food labels and nutrition information?...152"
## [153] "T1- Are you confident in understanding how to read food labels and nutrition information?...153"
## [154] "T2- Are you confident in understanding how to read food labels and nutrition information?...154"
## [155] "T0- Are you confident in understanding how to read food labels and nutrition information?...155"
## [156] "T1- Are you confident in understanding how to read food labels and nutrition information?...156"
## [157] "T2- Are you confident in understanding how to read food labels and nutrition information?...157"
## [158] "T1- In the past week, how many meals did you make using food from the program?...158"
## [159] "T2- In the past week, how many meals did you make using food from the program?...159"
## [160] "T1- In the past week, did you find it difficult to use the food in your meals?...160"
## [161] "T2- In the past week, did you find it difficult to use the food in your meals?...161"
## [162] "T1- In the past week, did you find it difficult to use the food in your meals?...162"
## [163] "T2- In the past week, did you find it difficult to use the food in your meals?...163"
## [164] "T2- Did you throw away any of the food from the deliveries?...164"
## [165] "T2- Did you throw away any of the food from the deliveries?...165"
## [166] "T1- Did the Magnet on Portion control impact your plate or portion sizes...166"
## [167] "T1- Since our last check in, have you had an appointment with a healthcare provider for your diabetes?...167"
## [168] "T2- Since our last check in, have you had an appointment with a healthcare provider for your diabetes?...168"
## [169] "T1- Since our last check in, have you had an appointment with a healthcare provider for your diabetes?...169"
## [170] "T2- Since our last check in, have you had an appointment with a healthcare provider for your diabetes?...170"
## [171] "PRE -- HbA1c Value (Last 3 Months)"
## [172] "POST -- HbA1c Value (Last 3 Months)"
## [173] "PRE -- Hosp Last 365 Days"
## [174] "POST -- Hosp Last 365 Days"
## [175] "PRE -- ED Vis Last 90 Days"
## [176] "POST -- ED Vis Last 90 Days"
## [177] "T1- Did the Magnet on Portion control impact your plate or portion sizes...177"
## [178] "T2- How much did this program help you feel more in control of your diabetes?...178"
## [179] "T2- Do you have suggestions or feedback to improve this program for others?...179"
## [180] "T2- Did you throw away any of the food from the deliveries?...180"
head(df) # first few rows
ggplot(df, aes(x = Age)) +
geom_histogram(binwidth = 5, fill = "steelblue", color = "white") +
labs(title = "Age Distribution", x = "Age (years)", y = "Count")
## Warning: Removed 5 rows containing non-finite outside the scale range
## (`stat_bin()`).
ggplot(df, aes(x = `Legal Sex`)) +
geom_bar(fill = "darkorange") +
labs(title = "Gender (Legal Sex)", x = "Legal Sex", y = "Count")
ggplot(df, aes(x = Race)) +
geom_bar(fill = "purple") +
labs(title = "Race", x = "Race", y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggplot(df, aes(x = Ethnicity)) +
geom_bar(fill = "seagreen") +
labs(title = "Ethnicity", x = "Ethnicity", y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggplot(df, aes(x = `HbA1c Value (Last 3 Months)`)) +
geom_histogram(binwidth = 0.5, fill = "tomato", color = "white") +
labs(title = "HbA1c Distribution", x = "HbA1c (%)", y = "Count")
## Warning: Removed 6 rows containing non-finite outside the scale range
## (`stat_bin()`).
# --- Age: summary table ---
age_table <- df %>%
summarise(
count = sum(!is.na(Age)),
mean_age = mean(Age, na.rm = TRUE),
median_age = median(Age, na.rm = TRUE),
min_age = min(Age, na.rm = TRUE),
max_age = max(Age, na.rm = TRUE)
)
# --- Gender (Legal Sex) ---
gender_table <- df %>%
count(`Legal Sex`, name = "n") %>%
mutate(percent = round(100 * n / sum(n), 1))
# --- Race ---
race_table <- df %>%
count(Race, name = "n") %>%
mutate(percent = round(100 * n / sum(n), 1))
# --- Ethnicity ---
ethnicity_table <- df %>%
count(Ethnicity, name = "n") %>%
mutate(percent = round(100 * n / sum(n), 1))
# --- A1c ---
a1c_table <- df %>%
summarise(
median_A1c = median(`HbA1c Value (Last 3 Months)`, na.rm = TRUE),
min_A1c = min(`HbA1c Value (Last 3 Months)`, na.rm = TRUE),
max_A1c = max(`HbA1c Value (Last 3 Months)`, na.rm = TRUE)
)
# show tables
age_table
gender_table
race_table
ethnicity_table
a1c_table
# 1) Normalize column names a bit (trim stray spaces)
names(df) <- trimws(names(df))
# 2) Find the three question columns by patterns (case-insensitive, handles couldn't/couldn’t)
find1 <- grep("worried.*run out", names(df), ignore.case = TRUE)
find2 <- grep("could.*afford.*not last|couldn'?t\\s*get more", names(df), ignore.case = TRUE)
find3 <- grep("last month.*skip meals|skip meals", names(df), ignore.case = TRUE)
# Safety checks: ensure we found one match each
stopifnot(length(find1) >= 1, length(find2) >= 1, length(find3) >= 1)
c(names(df)[find1[1]], names(df)[find2[1]], names(df)[find3[1]]) # print what was matched
## [1] "PS \"In the past 12 months, were you ever worried your food would run out before you had money to buy more?\"...23"
## [2] "PS \"In the past 12 months, did the food you could afford not last until the end of the month, and you couldn’t get more?\"...24"
## [3] "PS \"In the last month, did anyone in your household have to skip meals due to lack of food?\"...25"
# 3) Permanently rename those columns in df to VS1, VS2, VS3
names(df)[c(find1[1], find2[1], find3[1])] <- c("VS1","VS2","VS3")
# 4) Build the TRUE/FALSE matrix for "Yes" (robust to Y/YES/SI/SÍ and spaces/case)
norm <- function(x) trimws(toupper(as.character(x)))
yes_matrix <- data.frame(
VS1 = norm(df$VS1) %in% c("YES","Y","SI","SÍ"),
VS2 = norm(df$VS2) %in% c("YES","Y","SI","SÍ"),
VS3 = norm(df$VS3) %in% c("YES","Y","SI","SÍ")
)
# 5) Counts & percents for 0/1/2/3 “Yes”
yes_count <- rowSums(yes_matrix, na.rm = TRUE)
table_yes <- table(yes_count)
percent_yes <- round(100 * prop.table(table_yes), 1)
table_yes
## yes_count
## 0 1 2 3
## 6 8 26 27
percent_yes
## yes_count
## 0 1 2 3
## 9.0 11.9 38.8 40.3
# 6) Which questions were “Yes” (patterns: VS1, VS1, VS2, etc.)
yes_patterns <- apply(yes_matrix, 1, function(x) {
cols <- names(yes_matrix)[x]
if (length(cols) == 0) "None" else paste(cols, collapse = ", ")
})
pattern_table <- sort(table(yes_patterns[yes_count > 0]), decreasing = TRUE)
pattern_table
##
## VS1, VS2, VS3 VS1, VS2 VS1, VS3 VS2 VS1
## 27 20 5 4 2
## VS3 VS2, VS3
## 2 1
# If needed, install once:
if (!requireNamespace("readxl", quietly = TRUE)) install.packages("readxl")
if (!requireNamespace("dplyr", quietly = TRUE)) install.packages("dplyr")
if (!requireNamespace("tidyr", quietly = TRUE)) install.packages("tidyr")
if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")
if (!requireNamespace("scales", quietly = TRUE)) install.packages("scales")
library(readxl); library(dplyr); library(tidyr); library(ggplot2); library(scales)
# Helper to find first header that matches a pattern (case-insensitive)
find_col <- function(pattern, nms = names(df)) {
hits <- grep(pattern, nms, ignore.case = TRUE, value = TRUE)
if (length(hits) == 0) stop("No column matched: ", pattern)
hits[1]
}
# Helper for % labels in stacked bars
percent_format_1 <- function(x) scales::percent(x, accuracy = 1)
# ============================================================
# Block 1 — Glucose Monitoring
# Participants reported how often they checked blood sugar at T0, T1, and T2.
# We expect a shift from lower frequencies toward "Daily" over time.
# ------------------------------------------------------------
glucose_cols <- c(
find_col("^T0-\\s*In the past week.*check.*blood sugar"),
find_col("^T1-\\s*In the past week.*check.*blood sugar"),
find_col("^T2-\\s*In the past week.*check.*blood sugar")
)
glucose_long <- df %>%
select(all_of(glucose_cols)) %>%
setNames(c("T0","T1","T2")) %>%
pivot_longer(everything(), names_to="Timepoint", values_to="Frequency")
p_glucose <- ggplot(glucose_long, aes(Timepoint, fill = Frequency)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent_format_1) +
labs(title = "Glucose Monitoring Frequency Over Time",
y = "% of participants", x = NULL, fill = "Frequency")
p_glucose
# ============================================================
# Block 2 — Glycemic Control (Self-Reported)
# Patients answered whether sugars were usually in target range last week.
# Increasing % "Yes" suggests improved self-management.
# ------------------------------------------------------------
glycemic_cols <- c(
find_col("^T0-\\s*Were your sugar levels.*target range"),
find_col("^T1-\\s*Were your sugar levels.*target range"),
find_col("^T2-\\s*Were your sugar levels.*target range")
)
glycemic_long <- df %>%
select(all_of(glycemic_cols)) %>%
setNames(c("T0","T1","T2")) %>%
pivot_longer(everything(), names_to="Timepoint", values_to="TargetInRange")
p_glycemic <- ggplot(glycemic_long, aes(Timepoint, fill = TargetInRange)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent_format_1) +
labs(title = "Self-Reported In-Target Glucose (T0→T2)",
y = "% of participants", x = NULL, fill = "Response")
p_glycemic
# ============================================================
# Block 3 — Medication Adherence
# Patients reported whether they missed any diabetes medication doses in the past week.
# A decline in "Yes (missed)" over time indicates better adherence.
# ------------------------------------------------------------
miss_cols <- c(
find_col("^T0-\\s*In the past week.*miss any doses.*diabetes medication"),
find_col("^T1-\\s*In the past week.*miss any doses.*diabetes medication"),
find_col("^T2-\\s*In the past week.*miss any doses.*diabetes medication")
)
miss_long <- df %>%
select(all_of(miss_cols)) %>%
setNames(c("T0","T1","T2")) %>%
pivot_longer(everything(), names_to="Timepoint", values_to="MissedDose")
p_miss <- ggplot(miss_long, aes(Timepoint, fill = MissedDose)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent_format_1) +
labs(title = "Missed Diabetes Medication Doses (T0→T2)",
y = "% of participants", x = NULL, fill = "Response")
p_miss
# ============================================================
# Block 4 — Confidence in Food Choices
# Confidence in making food choices that help control blood sugar is expected to rise T0→T2.
# ------------------------------------------------------------
food_conf_cols <- c(
find_col("^T0-\\s*Are you.*Confident.*making food choices.*control.*blood sugar"),
find_col("^T1-\\s*Are you.*Confident.*making food choices.*control.*blood sugar"),
find_col("^T2-\\s*Are you.*Confident.*making food choices.*control.*blood sugar")
)
food_conf_long <- df %>%
select(all_of(food_conf_cols)) %>%
setNames(c("T0","T1","T2")) %>%
pivot_longer(everything(), names_to="Timepoint", values_to="Confidence")
p_food_conf <- ggplot(food_conf_long, aes(Timepoint, fill = Confidence)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent_format_1) +
labs(title = "Confidence in Food Choices (T0→T2)",
y = "% of participants", x = NULL, fill = "Response")
p_food_conf
# ============================================================
# Block 5 — Confidence in Reading Labels
# Nutrition-label literacy should strengthen across follow-ups.
# ------------------------------------------------------------
label_conf_cols <- c(
find_col("^T0-\\s*Are you.*confident.*read(ing)? food labels"),
find_col("^T1-\\s*Are you.*confident.*read(ing)? food labels"),
find_col("^T2-\\s*Are you.*confident.*read(ing)? food labels")
)
label_conf_long <- df %>%
select(all_of(label_conf_cols)) %>%
setNames(c("T0","T1","T2")) %>%
pivot_longer(everything(), names_to="Timepoint", values_to="LabelConfidence")
p_label_conf <- ggplot(label_conf_long, aes(Timepoint, fill = LabelConfidence)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent_format_1) +
labs(title = "Confidence Reading Food Labels (T0→T2)",
y = "% of participants", x = NULL, fill = "Response")
p_label_conf
# ============================================================
# Block 6 — Program Utilization
# T1/T2 capture how many meals were made with program food, ease of using it,
# and whether the portion-control magnet affected plate sizes.
# ------------------------------------------------------------
# ---------- Meals made with program food (T1 vs T2) ----------
# ---- Check what R really calls the meals columns ----
# --- Check and select the correct meals columns, then plot ---
# See all columns that mention "meals"
grep("meals", names(df), value = TRUE)
## [1] "T1- In the past week, how many meals did you make using food from the program?...38"
## [2] "T1- In the past week, did you find it difficult to use the food in your meals?...39"
## [3] "T2- In the past week, how many meals did you make using food from the program?...48"
## [4] "T2- In the past week, did you find it difficult to use the food in your meals?...49"
## [5] "T2 \"In the last month, did anyone in your household have to skip meals due to lack of food?\""
## [6] "PS \"In the last month, did anyone in your household have to skip meals due to lack of food?\"...122"
## [7] "T1- In the past week, how many meals did you make using food from the program?...158"
## [8] "T2- In the past week, how many meals did you make using food from the program?...159"
## [9] "T1- In the past week, did you find it difficult to use the food in your meals?...160"
## [10] "T2- In the past week, did you find it difficult to use the food in your meals?...161"
## [11] "T1- In the past week, did you find it difficult to use the food in your meals?...162"
## [12] "T2- In the past week, did you find it difficult to use the food in your meals?...163"
# Replace the names below with the exact ones shown by grep output:
# ---------- Meals made with program food (T1 vs T2) ----------
# Robust: find the 2 columns by pattern, then select by their actual names
# ---------- Meals made with program food (T1 vs T2) ----------
meals_cols <- c(
"T1- In the past week, how many meals did you make using food from the program?...158",
"T2- In the past week, how many meals did you make using food from the program?...159"
)
meals_long <- df %>%
dplyr::select(dplyr::all_of(meals_cols)) %>%
setNames(c("T1","T2")) %>%
tidyr::pivot_longer(dplyr::everything(),
names_to = "Timepoint",
values_to = "MealsFromProgram")
ggplot(meals_long, aes(x = MealsFromProgram)) +
geom_bar() +
facet_wrap(~ Timepoint, nrow = 1) +
labs(title = "Meals Made Using Program Food (T1 vs T2)",
x = "# meals in past week", y = "Count")
## Warning: Removed 10 rows containing non-finite outside the scale range
## (`stat_count()`).
idx_meals <- grep(
"^T[12]-\\s*In the past week.*how many meals.*(program|program food)",
names(df), ignore.case = TRUE
)
meals_cols <- names(df)[idx_meals]
meals_cols
## [1] "T1- In the past week, how many meals did you make using food from the program?...38"
## [2] "T2- In the past week, how many meals did you make using food from the program?...48"
## [3] "T1- In the past week, how many meals did you make using food from the program?...158"
## [4] "T2- In the past week, how many meals did you make using food from the program?...159"
# Treat responses as categories (works even if entries are text like “More than 10”)
ggplot(meals_long, aes(x = MealsFromProgram)) +
geom_bar() +
facet_wrap(~ Timepoint, nrow = 1) +
labs(title = "Meals Made Using Program Food (T1 vs T2)",
x = "# meals in past week", y = "Count")
## Warning: Removed 10 rows containing non-finite outside the scale range
## (`stat_count()`).
# ============================================================
# Block 7 — Healthcare Engagement
# Follow-ups ask whether participants had a diabetes appointment since last check-in.
# Increases suggest improved engagement with medical care.
# ------------------------------------------------------------
appt_cols <- c(
find_col("^T1-\\s*Since our last check.*appointment.*diabetes"),
find_col("^T2-\\s*Since our last check.*appointment.*diabetes")
)
appt_long <- df %>%
select(all_of(appt_cols)) %>%
setNames(c("T1","T2")) %>%
pivot_longer(everything(), names_to="Timepoint", values_to="HadAppt")
p_appt <- ggplot(appt_long, aes(Timepoint, fill = HadAppt)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent_format_1) +
labs(title = "Diabetes Appointment Since Last Check-In",
y = "% of participants", x = NULL, fill = "Response")
p_appt
# ============================================================
# Block 8 — Clinical Outcomes
# Pre/Post HbA1c (and acute utilization) gauge clinical impact of the program.
# Lower post-A1c or fewer ED/hospital days indicate improvement.
# ------------------------------------------------------------
# ---- Block 8: Clinical Outcomes ----
# Use exact column names as they appear in your data
col_a1c_pre <- "PRE -- HbA1c Value (Last 3 Months)"
col_a1c_post <- "POST -- HbA1c Value (Last 3 Months)"
a1c_long <- df %>%
dplyr::transmute(PRE = .data[[col_a1c_pre]],
POST = .data[[col_a1c_post]]) %>%
tidyr::pivot_longer(dplyr::everything(),
names_to = "Time",
values_to = "A1c")
ggplot(a1c_long, aes(Time, A1c)) +
geom_boxplot() +
labs(title = "HbA1c: PRE vs POST",
x = NULL, y = "HbA1c (%)")
## Warning: Removed 10 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
a1c_long <- df %>%
transmute(PRE = .data[[col_a1c_pre]],
POST = .data[[col_a1c_post]]) %>%
pivot_longer(everything(), names_to = "Time", values_to = "A1c")
p_a1c <- ggplot(a1c_long, aes(Time, A1c)) +
geom_boxplot() +
labs(title = "HbA1c: PRE vs POST", x = NULL, y = "HbA1c (%)")
p_a1c
## Warning: Removed 10 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
# (Optional) ED visits / Hospital days pre vs post (if present)
ed_pre <- grep("^PRE-\\s*ED", names(df), ignore.case = TRUE, value = TRUE)
ed_post <- grep("^POST-\\s*ED", names(df), ignore.case = TRUE, value = TRUE)
hosp_pre <- grep("^PRE-\\s*Hosp", names(df), ignore.case = TRUE, value = TRUE)
hosp_post<- grep("^POST-\\s*Hosp",names(df), ignore.case = TRUE, value = TRUE)
if (length(ed_pre) & length(ed_post)) {
ed_long <- df %>%
transmute(PRE = .data[[ed_pre[1]]], POST = .data[[ed_post[1]]]) %>%
pivot_longer(everything(), names_to="Time", values_to="ED_Visits")
p_ed <- ggplot(ed_long, aes(Time, ED_Visits)) +
stat_summary(fun = mean, geom = "bar", fill = "grey70") +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .2) +
labs(title = "ED Visits: PRE vs POST (Mean ± SE)", x = NULL, y = "Visits")
p_ed
}
if (length(hosp_pre) & length(hosp_post)) {
hosp_long <- df %>%
transmute(PRE = .data[[hosp_pre[1]]], POST = .data[[hosp_post[1]]]) %>%
pivot_longer(everything(), names_to="Time", values_to="Hosp_Days")
p_hosp <- ggplot(hosp_long, aes(Time, Hosp_Days)) +
stat_summary(fun = mean, geom = "bar", fill = "grey70") +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .2) +
labs(title = "Hospital Days: PRE vs POST (Mean ± SE)", x = NULL, y = "Days")
p_hosp
}
# ============================================================
# Block 9 — Education Impact / Sharing
# T1/T2 ask whether the magnet helped with portion sizes and whether information
# was shared with others, indicating reach and behavior influence.
# ------------------------------------------------------------
share_cols <- grep("^T[12]-\\s*Did you share.*", names(df), ignore.case = TRUE, value = TRUE)
if (length(share_cols)) {
# Expect two columns (T1 & T2); keep first two if more
share_cols <- share_cols[seq_len(min(2, length(share_cols)))]
share_long <- df %>%
select(all_of(share_cols)) %>%
setNames(sub("^.*(T[12]).*", "\\1", share_cols)) %>%
pivot_longer(everything(), names_to="Timepoint", values_to="Shared")
p_share <- ggplot(share_long, aes(Timepoint, fill = Shared)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent_format_1) +
labs(title = "Shared Program Information (T1 vs T2)",
y = "% of participants", x = NULL, fill = "Response")
p_share
}
ls(pattern = "^p_")
## [1] "p_a1c" "p_appt" "p_food_conf" "p_glucose" "p_glycemic"
## [6] "p_label_conf" "p_miss"
p_age <- ggplot(df, aes(x = Age)) +
geom_histogram(binwidth = 5, color = "white") +
labs(title = "Age Distribution", x = "Age (years)", y = "Count")
# Gender (Legal Sex)
p_gender <- ggplot(df, aes(x = `Legal Sex`)) +
geom_bar() +
labs(title = "Gender (Legal Sex)", x = "Legal Sex", y = "Count")
# Race
p_race <- ggplot(df, aes(x = Race)) +
geom_bar() +
labs(title = "Race", x = "Race", y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Ethnicity
p_ethnicity <- ggplot(df, aes(x = Ethnicity)) +
geom_bar() +
labs(title = "Ethnicity", x = "Ethnicity", y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Print them so they render in the knitted report
p_age; p_gender; p_race; p_ethnicity
## Warning: Removed 5 rows containing non-finite outside the scale range
## (`stat_bin()`).
# ============================================================
# Demographic Visualizations
# ============================================================
# ---- Age Distribution ----
p_age <- ggplot(df, aes(x = Age)) +
geom_histogram(binwidth = 5, fill = "steelblue", color = "white") +
labs(title = "Age Distribution", x = "Age (years)", y = "Count")
p_age
## Warning: Removed 5 rows containing non-finite outside the scale range
## (`stat_bin()`).
# ---- Gender ----
p_gender <- ggplot(df, aes(x = `Legal Sex`)) +
geom_bar(fill = "darkorange") +
labs(title = "Gender Distribution", x = "Legal Sex", y = "Count")
p_gender
# ---- Race ----
p_race <- ggplot(df, aes(x = Race)) +
geom_bar(fill = "purple") +
labs(title = "Race Distribution", x = "Race", y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
p_race
# ---- Ethnicity ----
p_ethnicity <- ggplot(df, aes(x = Ethnicity)) +
geom_bar(fill = "seagreen") +
labs(title = "Ethnicity Distribution", x = "Ethnicity", y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
p_ethnicity