Search code examples
rstatisticsanovachi-squaredt-test

T-test, anova, chi-square in R


    1    2
y1  48  44
y2  38  39
y3  49  56
y4  3   4
y5  55  28
y6  99  101
y7  121 120
y8  2   6

1) Given this descriptive statistics where 1 and 2 are the outcome (Y = 1 or 2 ) and Y1 - Y8 are the variables, I want to perform independent t-test using unequal variance. Y4 and Y8 are binary variables, and I need to perform chi-square. I want the results from these tests as my third column to see which variable is a driving factor of the group distinction (Y = 1 or 2). How would I be able to do this in R?

2) If the outcome changes to three categories (Y = 1, 2, and 3), how can I perform ANOVA for continuous variables and chi-square for Y4 and Y8 in R?



Solution

  • First of all, you shouldn't mix the binary variables with the rest of the measurements. I will start by separating the input dataframe in two dataframes.

    df2 <- df1[c(4, 8), ]
    df3 <- df1[-c(4, 8), ]
    

    Now the tests. The t.test will need the data in long format, see this question for other ways of reshaping the dataset.

    chisq.test(df2)
    
    long <- reshape2::melt(df3)
    t.test(value ~ variable, long)
    

    Data in dput format.

    df1 <-
    structure(list(`1` = c(48L, 38L, 49L, 3L, 55L, 
    99L, 121L, 2L), `2` = c(44L, 39L, 56L, 4L, 28L, 
    101L, 120L, 6L)), class = "data.frame", 
    row.names = c("y1", "y2", "y3", "y4", "y5", 
    "y6", "y7", "y8"))