2

I would like to have a table as ouptut where I have the t-statistics between the difference in means of certain variables and based on two specific subsets of my data.

I have the following data:

structure(list(Name = c("A", "A", "A", "A", "B", "B", "B", "B", 
"C", "C", "C", "C", "D", "D", "D", "D"), Date = c("20.10.2018", 
"30.09.2018", "25.11.2019", "23.10.2020", "20.03.2018", "30.07.2018", 
"25.08.2019", "23.10.2020", "20.12.2018", "30.01.2018", "25.02.2019", 
"23.06.2020", "20.11.2018", "30.12.2018", "25.11.2019", "23.09.2020"
), Return = c(0.01, 0.05, 0.08, 0.07, 0.04, 0.03, 0.01, 0.03, 
0.03, 0.05, 0.06, 0.07, 0.07, 0.04, 0.06, 0.08), Age = c(5L, 
5L, 6L, 7L, 8L, 8L, 9L, 10L, 4L, 4L, 5L, 6L, 1L, 1L, 2L, 3L), 
    Size = c(53336L, 75768L, 86548L, 94567L, 40234L, 40240L, 
    50243L, 60352L, 5069L, 6069L, 7092L, 8024L, 2456L, 3046L, 
    4056L, 5600L), Rating = c(1L, 1L, 1L, 2L, 5L, 5L, 3L, NA, 
    4L, 5L, 4L, 5L, NA, 4L, 5L, 4L)), class = "data.frame", row.names = c(NA, 
-16L))

More specifically, I would like to have a table where I have t-statistics for each differences of means between the variables Return, Age and Size for the observations with a Rating of 1 and 5. The t-statistics should be the column between Rating 1 and Rating 5 and should include the stars that indicate the p-value.

I tried using the t.test function but I have difficulties using it with subgroups only and create the t-statistics column in the middle between Rating 1 and Rating 5.

The output should have the layout like this:

structure(list(c("Return", "Age", "Size"), `Mean Rating 1` = c(NA, 
NA, NA), `t-statistics including p-value (indicated as stars)` = c(NA, 
NA, NA), `Mean Rating 5` = c(NA, NA, NA)), class = "data.frame", row.names = c(NA, 
-3L))

Could someone help me here with the code?

Thank you a lot in advance.

EDIT 22.04.2022:

Question 1: How would I need to adjust the code in the answer if I would like the output to be the following (there are no values now but just to illustrate the layout I would like to have):

structure(list(c("Return", "Age", "Size"), `Mean Rating 1` = c(NA, 
NA, NA), `Mean Rating2` = c(NA, NA, NA), `Mean Rating 3` = c(NA, 
NA, NA), `Mean Rating 4` = c(NA, NA, NA), `Mean Rating 5` = c(NA, 
NA, NA), `Mean Rating NA` = c(NA, NA, NA), `Difference in means Rating 5 and Rating 1` = c(NA, 
NA, NA), `p-value for differences in means Rating 5 and Rating 1` = c(NA, 
NA, NA), `stars for p-value for differences in means Rating 5 and Rating 1` = c(NA, 
NA, NA)), class = "data.frame", row.names = c(NA, -3L))

Question 2: When I want to compare the differences in means between two groups, is it better to use the t-test or F-test? I have chosen the t-test since as far as I know if I want to compare the means between two groups t-test is the right test. The F-test is better to use if I want to compare the two standard deviations of the two groups. Is my understanding right?

remo
  • 365
  • 1
  • 10

1 Answers1

3

You may easily loop over a subset=.

t(with(mtcars, sapply(unique(cyl), \(i) t.test(am, subset=cyl == i))))
#      statistic parameter p.value      conf.int  estimate null.value stderr     alternative method              data.name
# [1,] 4.605489  31        6.632258e-05 numeric,2 0.40625  0          0.08820997 "two.sided" "One Sample t-test" "am"     
# [2,] 4.605489  31        6.632258e-05 numeric,2 0.40625  0          0.08820997 "two.sided" "One Sample t-test" "am"     
# [3,] 4.605489  31        6.632258e-05 numeric,2 0.40625  0          0.08820997 "two.sided" "One Sample t-test" "am"  

More specific for your data you could do this:

tcols <- c('Return', 'Age', 'Size')
r <- t(with(subset(dat, Rating %in% c(1, 5)), 
     sapply(setNames(tcols, tcols), \(i) unlist(
       t.test(reformulate('Rating', i))[
         c('estimate', 'statistic', 'p.value')]
       ))))
cbind(as.data.frame(r),
      ' '=c("   ", "*  ", "** ", "***")[
        rowSums(outer(r[, 'p.value'], c(Inf, 0.05, 0.01, 0.001), `<`))])
#        estimate.mean in group 1 estimate.mean in group 5 statistic.t   p.value    
# Return             4.666667e-02                     0.05  -0.1552301 0.8883096    
# Age                5.333333e+00                     5.60  -0.2198599 0.8353634    
# Size               7.188400e+04                 19724.60   4.0457818 0.0109848 *  

Note R >= 4.1 used.

Edit

as.data.frame(t(with(subset(dat, Rating %in% c(1, 5)), 
       sapply(setNames(tcols, tcols), \(i) unlist(
         t.test(reformulate('Rating', i))[
           c('estimate', 'statistic', 'p.value')]
       ))))) |>
  {\(.) cbind(mean.diff.5.1=apply(.[1:2], 1, diff), .[3:4])}() |> 
  cbind(' '=c("   ", "*  ", "** ", "***")[
          rowSums(outer(r[, 'p.value'], c(Inf, 0.05, 0.01, 0.001), `<`))],
        `colnames<-`(t(aggregate(. ~ Rating, dat[3:6], mean)[-1]), 
                     paste0('mean.rating.', 1:5))) |>
  {\(.) .[c(5:9, 1:4)]}()
#        mean.rating.1 mean.rating.2 mean.rating.3 mean.rating.4 mean.rating.5 mean.diff.5.1 statistic.t   p.value    
# Return  4.666667e-02          0.07          0.01        0.0525          0.05  3.333333e-03  -0.1552301 0.8883096    
# Age     5.333333e+00          7.00          9.00        3.2500          5.60  2.666667e-01  -0.2198599 0.8353634    
# Size    7.188400e+04      94567.00      50243.00     5201.7500      19724.60 -5.215940e+04   4.0457818 0.0109848 * 
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • I could now make it work also with my original data :). Thank you again! How would it be possible to have the rownames included in the output? So that row #1 has the name Return, #2 Age and #3 Size? This way it would be easier to read. – remo Apr 08 '22 at 09:24
  • 1
    @remo Finally found your comment, sorry. When we give the `*apply` family functions a named input, they also throw something named, see update with `setNames`. – jay.sf Apr 08 '22 at 16:24
  • It is me again. I updated my question above since I know from my question yesterday that you are a real expert in statistics. Therefore, I would really appreciate if you could help me here again! – remo Apr 22 '22 at 17:12
  • @remo Thanks for the flowers. We may calculate the mean differences using `diff` in `apply`, and the mean ratings using `aggregate`. We should use syntactically valid names, see `?make.names`, choose them to be able to identify which is what, different from publishing. Hope you are still able to understand the code, it's getting awkward :D – jay.sf Apr 22 '22 at 17:41
  • Thank you jay.sf for responding so quickly! I will have a detailed look at it and try it with my real data. – remo Apr 22 '22 at 17:43
  • Do you may know why I get the error message: "Error in .subset2(x, i, exact = exact) : attempt to select less than one element in integerOneIndex"? Even when I run the initial code in your first answer. – remo Apr 22 '22 at 20:06
  • @remo Weird. Could you update your question with a small example that reproduces the issue? – jay.sf Apr 22 '22 at 20:16
  • I tried many things to make it work and I found the mistake. I still had a column called Rating in my tcols. Now I looking at the new part of the code. – remo Apr 23 '22 at 06:48
  • What would your answer be to Question 2 in my edited version of the question from yesterday? – remo Apr 23 '22 at 11:53
  • 1
    @remo Either `t.test` for continuous data such as `Return`, or `chisq.test` for categorical data like `Rating`. Here a brief reading https://www.statology.org/chi-square-test-vs-t-test/ Statistical questions actually are off topic on Stack Overflow, but we have [Cross Validated](https://stats.stackexchange.com/help/on-topic). – jay.sf Apr 23 '22 at 12:07