Home > database >  Expss and chisquare test on row percentage
Expss and chisquare test on row percentage

Time:03-14

How to perform a Chi-squared test on a row percent using expss package? I know the Significance testing on column percent should be applied on the result of tab_stat_cpct with total row. But I have a table like this and my Chi-squared p-value IS blank IN MY TABLE.

d=mtcars
banner <- d %>%  tab_cells(vs, am)
# column percentage significacne
tab_cpct_sig=. %>% 
tab_stat_cpct() %>%
 tab_total_row_position("below") %>% 
    tab_total_statistic("u_rpct")


Table1 = function (Q, banner) {
    eval.parent(substitute(
        {
            banner %>%
                tab_cols (Q) %>%
                 tab_stat_rpct() %>% 
          tab_cols(total(Q)) %>% 
          tab_stat_cases() %>% 
        tab_last_sig_cases(sig_level = 0.05) %>%
        tab_pivot(stat_position = "outside_columns")  %>%  
               
            
            set_caption("Tableau 1") #%>%
         
           
              
        }
    ))
}
A1=Table1(gear,banner)
A1

CodePudding user response:

Currently expss only supports chi-square test on the entire table. However, we can make a function to add rowwise chi-square statistic:

chi_sq_row = function(df){
    totals = grepl("#", df[[1]])
    df_matr = as.matrix(df[,-1][!totals])
    df_matr[is.na(df_matr)] = 0
    df[!totals,"chisq"] = apply(df_matr, 1,
                                   function(each_row) chisq.test(
                                       head(each_row, -1)*tail(each_row, 1)/100
                                   )$statistic
    )
    df[!totals, "pvalue"] = apply(df_matr, 1,
                                   function(each_row) chisq.test(
                                       head(each_row, -1)*tail(each_row, 1)/100
                                   )$p.value
    )
    df
}

Table1 = function (Q, banner) {
    eval.parent(substitute(
        {
            banner %>%
                tab_cols (Q) %>%
                tab_stat_rpct() %>% 
                tab_cols(total(Q)) %>% 
                tab_stat_cases() %>% 
                tab_pivot(stat_position = "outside_columns")  %>%  
                chi_sq_row() %>% 
                set_caption("Tableau 1")
            
            
            
        }
    ))
}
A1=Table1(gear,banner)
A1

CodePudding user response:

I thank you for your SUPPORT. I think this function resolveS the problem by adding two columns in the table #chisq and #pvalue, BUT I GET THIS WARNING:

Warning in chisq.test(head(each_row, -1) * tail(each_row, 1)/100): Chi-squared

approximation may be incorrect

  • Related