---
title: "Ncube fitness training dashboard"
output:
flexdashboard::flex_dashboard:
vertical_layout: fill
orientation: rows
source_code: embed
---
<!--- .css scripts can be added directly into html markdown docs --->
<style type="text/css">
.navbar-inverse .navbar-nav>.active>a, .navbar-inverse .navbar-nav>.active>a:hover, .navbar-inverse .navbar-nav>.active>a:focus {
color:gold!important;
background-color: #303030;
border-color: #002a4c;
font-weight: 900;
}
.chart-title {
border-bottom: 1px solid #d7d7d7;
color: orange;
font-size: 20px;
font-weight: 700;
padding: 7px 10px 4px;
}
.heading-level1 { /* chart_title */
color: #00467f;
font-size: 18px;
font-weight: 500;
}
h4, .h4 {
color: #002a4c;
font-size: 15px;
font-weight: 550;
}
a:visited {
color: rgb(50%, 0%, 50%);
}
</style>
<!--- the example above changes the header bars colors --->
```{r}
pacman::p_load("tidyverse",
"tidymodels",
"magrittr",
"here",
"scales",
"glmnet",
"stacks",
"janitor",
"finetune",
"vip",
"data.table",
"DT",
"alluvial",
"extrafont",
"gt",
"gtsummary",
"flexdashboard")
```
Overview
=======================================================================
Row
-----------------------------------------------------------------------
```{r}
df<-readr::read_csv("fitness_class_2212.csv") #readin
df<-df|>
mutate_at(c(5,6,7),as.factor) |> # change these columns to factors
mutate(attended2=ifelse(attended==1,"yes","no"),
day_of_week =
fct_collapse(day_of_week,Wed=c("Wed","Wednesday"),
Fri=c("Fri.","Fri"), # collapse redundant levels
Mon=c("Mon","Monday")),
category=fct_collapse(category,unknown=c("-")), # add category(unknown)
days_before=readr::parse_number(days_before)) # leave only numbers in the dataset
```
### Total number of members recorded {.value-box}
```{r}
wardTotal <- nrow(df)
valueBox(wardTotal, "Total number of members recorded", icon="fa-desktop", color = ggthemes::tableau_color_pal()(1))
```
### maximum months as member {.value-box}
```{r}
bcTotal <- max(df$months_as_member, na.rm = TRUE)
valueBox(bcTotal, "maximum number of months", icon="fa-desktop", color = "orange")
```
### attendance proportion {.value-box}
```{r}
perCU <- round(mean(df$attended)*100,2)
valueBox(paste(perCU, "%"), "Proportion of members that attended", icon="fa-desktop", color = tvthemes::avatar_pal()(1))
```
### mean weight of attendees
```{r}
cu_rolling_avg <- mean(df$weight, na.rm = TRUE) #7 day average CU Boulder
gauge(value = cu_rolling_avg,
min = min(df$weight,na.rm = T),
max = max(df$weight,na.rm = T),
sectors = gaugeSectors(success=c(55,85),
warning=c(86,100),
danger=c(101,200)))
```
column {data-width=400}
-----------------------------------------------------------------------
### Proportion of attendees
```{r,fig.height=6,fig.width=10}
loadfonts(quiet=TRUE)
iv_rates <- df |>
mutate(attended=ifelse(attended2==1,"yes","no"),
attended=as.factor(attended2)) |>
group_by(attended) |>
summarize(count = n()) |>
mutate(prop = count/sum(count)) |>
ungroup()
plot<-iv_rates |>
ggplot(aes(x=attended, y=prop, fill=attended)) +
geom_col(color="black",width = 0.5)+
theme(legend.position="bottom") +
geom_label(aes(label=scales::percent(prop)), color="white") +
labs(
title = "attendance ratio",
subtitle = "Fitness analysis",
y = "proportion(%)",
x = "attendance",
fill="attendance",
caption="B.Ncube::Data enthusiast") +
scale_y_continuous(labels = scales::percent)+
tvthemes::scale_fill_kimPossible()+
tvthemes::theme_theLastAirbender(title.font="Slayer",
text.font = "Slayer")+
theme(legend.position = 'right')
plot
```
### attendence status vs numeric variables
```{r,fig.height=6,fig.width=10}
subset <- df |>
dplyr::mutate(days_before=as.numeric(days_before),
attended = ifelse(attended==1,"yes","no")) |>
dplyr::select(days_before,months_as_member,weight,attended)
# Bring in external file for visualisations
source('functions/visualisations.R')
# Use plot function
plot <- histoplotter(subset, attended,
chart_x_axis_lbl = "attendence status",
chart_y_axis_lbl = 'Measures',
boxplot_color = 'navy',
boxplot_fill = '#89CFF0',
box_fill_transparency = 0.2)
# Add extras to plot
plot +
tvthemes::theme_theLastAirbender() +
tvthemes::scale_color_attackOnTitan()+
theme(legend.position = 'top')
```
Histograms
===
Row
---
### histogram of months as a member
```{r,fig.height=6,fig.width=10}
library(statip)
min_val <- min(df$months_as_member)
max_val <- max(df$months_as_member)
mean_val <- mean(df$months_as_member)
med_val <- median(df$months_as_member)
mod_val <- mfv(df$months_as_member)
id_02_hist <- df |>
ggplot() +
geom_histogram(aes(x = months_as_member),
fill = "firebrick", alpha = 0.66) +
labs(title = "months_as_member distribution") +
theme(plot.title = element_text(hjust = 0.5),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())+
ggthemes::scale_fill_tableau()+
tvthemes::theme_theLastAirbender(title.font="Slayer",text.font = "Slayer")+
geom_vline(xintercept = min_val, color = 'gray33', linetype = "dashed", size = 1.3)+
geom_vline(xintercept = mean_val, color = 'cyan', linetype = "dashed", size = 1.3)+
geom_vline(xintercept = med_val, color = 'red', linetype = "dashed", size = 1.3 )+
geom_vline(xintercept = mod_val, color = 'yellow', linetype = "dashed", size = 1.3 )+
geom_vline(xintercept = max_val, color = 'gray33', linetype = "dashed", size = 1.3 )
id_02_hist
```
### log transformed data
```{r,fig.height=6,fig.width=10}
id_02_log_hist <- df |>
ggplot() +
geom_histogram(aes(x = months_as_member),
fill = "firebrick", alpha = 0.66) +
labs(title = "months_as_member log distribution") +
scale_x_log10() +
theme(plot.title = element_text(hjust = 0.5),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())+
ggthemes::scale_fill_tableau()+
tvthemes::theme_theLastAirbender(title.font="Slayer",text.font = "Slayer")
id_02_log_hist
```
Counts
===
Row
---
### category ,attendance and time
```{r,fig.height=6,fig.width=10}
df|>
dplyr::mutate(attended = ifelse(attended==1,"yes","no"))|>
dplyr::group_by(attended,category,time) |>
dplyr::summarize(count=n()) |>
dplyr::mutate(prop=count/sum(count)) |>
ggplot2::ggplot(aes(x = category, y = count,fill=attended))+
ggplot2::geom_bar(stat = "identity")+
tvthemes::scale_fill_avatar()+
ggplot2::facet_wrap(~time,scales="free")+
tvthemes::theme_theLastAirbender(title.font="Slayer",
text.font = "Slayer")+
ggplot2::theme(legend.position = 'right',
axis.text.x = element_text(face="bold",
color="brown",
size=8,
angle=45))
```
Chi-squared tests
===
Row
---
### relationship between time,category and attendance
````{r,fig.height=6,fig.width=10}
df|>
mutate(weight = replace_na(weight, mean(weight, na.rm = T)),
attended = ifelse(attended==1,"yes","no"))|>
select(attended,time,category) |>
tbl_summary(
by = attended,
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} / {N} ({p}%)"),
label = time ~ "time of the day")|>
add_p(test = all_continuous() ~ "t.test",
pvalue_fun = function(x) style_pvalue(x, digits = 2))|>
modify_header(statistic ~ "**Test Statistic**")|>
bold_labels()|>
modify_fmt_fun(statistic ~ style_sigfig)
```
### relationship between day and attendance
````{r,fig.height=6,fig.width=10}
df|>
mutate(weight = replace_na(weight, mean(weight, na.rm = T)),
attended = ifelse(attended==1,"yes","no"))|>
select(attended,day_of_week) |>
tbl_summary(
by = attended,
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} / {N} ({p}%)"),
label = day_of_week ~ "time of week")|>
add_p(test = all_continuous() ~ "t.test",
pvalue_fun = function(x) style_pvalue(x, digits = 2))|>
modify_header(statistic ~ "**Test Statistic**")|>
bold_labels()|>
modify_fmt_fun(statistic ~ style_sigfig)
```
t-tests
===
Row
---
### relationship between variables
````{r,fig.height=6,fig.width=10}
df|>
mutate(weight = replace_na(weight, mean(weight, na.rm = T)),
attended = ifelse(attended==1,"yes","no"))|>
select(attended,weight,months_as_member) |>
tbl_summary(
by = attended,
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} / {N} ({p}%)"),
label = months_as_member ~ "number of months as a member")|>
add_p(test = all_continuous() ~ "t.test",
pvalue_fun = function(x) style_pvalue(x, digits = 2))|>
modify_header(statistic ~ "**Test Statistic**")|>
bold_labels()|>
modify_fmt_fun(statistic ~ style_sigfig)
```
Data
==============================================================
### fitness training dataset
```{r}
datatable(df)
```