I am generating a cross table between two variables:
Sex (man and woman) and Pain_type (acute, severe, resolved, absent)
With expss package and custom tables feature.
The table contains cases made by tab_stat_cases and percentages made by per column tab_stat_cpct.
I am also doing a z test for significant differences between the percentages per column with the tab_last_sig_cpct function. However, I can’t find a way to generate additional columns with the z and p values because the significance function only does the calculation and does not show me those values.
The code I am using is the following:
Data|>
tab_cells("|"=unvr(Pain_type))|>
tab_cols(total(label = "Total| |"), Sex)|>
tab_stat_cases(label = "N")|>
tab_last_add_sig_labels() |>
tab_stat_cpct(label = "%")|>
tab_last_sig_cpct()|>
tab_pivot(stat_position = "inside_columns")
I have been trying to solve it with the tab_stat_fun_df
function but it still doesn’t work for me.
Data|>
tab_cells("|"=unvr(Pain_type))|>
tab_cols(total(label = "Total| |"), Sex)|>
tab_stat_cases(label = "N")|>
tab_last_add_sig_labels() |>
tab_stat_cpct(label = "%")|>
tab_last_sig_cpct()|>
tab_stat_fun_df(function(x){
p_unified <- sum(data$Freq) / sum(data$Total)
data$Z_value <- mapply(function(frec, total){
p1 <- frec / total
p2 <- data$Freq[1] / data$Total[1]
n1 <- total
n2 <- data$Total[1]
z <- (p1 - p2) / sqrt(p_unified * (1 - p_unified) * (1/n1 + 1/n2))
return(z)},
data$Freq, data$Total)
}, label = "Z value")|>
tab_pivot(stat_position = "inside_columns")
I hope to get a table like this
table of significant differences between percentages