Pared t-test, "mu" must be a single number

Hello! I'm trying to do a paired t-test to see if there is a difference between the before and after samples (-5 and 0). But I only get the response that "mu" must be a single number. But how do I make mu to a single number, or how should I re-format my data to make this happen? Not sure what mu stands for either.

A subset of the data is down below.

Replica Bay Boat Time Depth Average S.D. Difference

3 1 Väst Small -5 1.5 1.03 0.0499 0
4 1 Väst Small 0 1.5 2.32 0.101 1.29

I checked for normal distribution before starting with the t-test by using a shapiro.test.

The code I'm using is below. But using the code with my data only results in "Error in t.test.default(Dist.T.15.S.V.R1, x = "Time", y = "Average", paired = TRUE, :
'mu' must be a single number"

ggboxplot(Dist.T.15.S.V.R1, x = "Time", y = "Average", palette = c("#00AFBB", "#E7B800"), order = c("-5", "0"), ylab = "Turbidity", xlab = "Time")
t.test(Dist.T.10.S.V.R1, x = "Time", y = "Average", paired = TRUE, alternative = "two.sided")

I understand that you want to compare the values in the Average column paired according to the Time. I invented a small data set since you only provided two rows of data and I did the analysis in two ways. In the first I used the t.test "formula interface" to express that Average should be compared at different values of Time. In the second version, I reshaped the data so that each row contains the associated Average values at Time = -5 and Time = 0. Compare the original DF to the reshaped DFwide to see how the values were repositioned. You can see that the t.test results are identical.

DF <- data.frame(Replica = rep(1:10, each = 2), Bay = "Vast", 
                 Boat = "Small", Time = rep(c(-5, 0),10), 
                 Depth = 1.5, Average = rnorm(20, mean = 2, sd = 1), 
                 S.D. = runif(20),  Difference = runif(20))

DF
#>    Replica  Bay  Boat Time Depth   Average      S.D.  Difference
#> 1        1 Vast Small   -5   1.5 2.6417051 0.3804981 0.089201455
#> 2        1 Vast Small    0   1.5 2.4371311 0.1179992 0.483146503
#> 3        2 Vast Small   -5   1.5 1.5411826 0.3809335 0.661100151
#> 4        2 Vast Small    0   1.5 2.1577250 0.8123062 0.858314304
#> 5        3 Vast Small   -5   1.5 2.8430718 0.1873026 0.342631383
#> 6        3 Vast Small    0   1.5 2.0120656 0.2718236 0.501260335
#> 7        4 Vast Small   -5   1.5 1.6510801 0.1848850 0.022939839
#> 8        4 Vast Small    0   1.5 1.1311523 0.9912124 0.007652733
#> 9        5 Vast Small   -5   1.5 2.7574756 0.3490825 0.205818067
#> 10       5 Vast Small    0   1.5 2.0995803 0.5960672 0.529268654
#> 11       6 Vast Small   -5   1.5 2.3222794 0.4882487 0.667002798
#> 12       6 Vast Small    0   1.5 2.1940413 0.4801476 0.122623173
#> 13       7 Vast Small   -5   1.5 1.4695869 0.5236233 0.350077632
#> 14       7 Vast Small    0   1.5 2.5456604 0.1529702 0.534559767
#> 15       8 Vast Small   -5   1.5 1.9984230 0.9445697 0.793341777
#> 16       8 Vast Small    0   1.5 0.1577001 0.9629901 0.281684059
#> 17       9 Vast Small   -5   1.5 2.9099732 0.3908567 0.981132287
#> 18       9 Vast Small    0   1.5 2.1025757 0.4880733 0.501476942
#> 19      10 Vast Small   -5   1.5 2.3203045 0.5813115 0.046676651
#> 20      10 Vast Small    0   1.5 1.7892412 0.2017480 0.904602418
t.test(Average ~ Time, paired = TRUE, data = DF)
#> 
#>  Paired t-test
#> 
#> data:  Average by Time
#> t = 1.502, df = 9, p-value = 0.1674
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#>  -0.1937574  0.9593993
#> sample estimates:
#> mean of the differences 
#>               0.3828209

library(dplyr)
library(tidyr)
DFwide <- DF |> select(-S.D., -Difference) |> 
pivot_wider(names_from = Time, values_from = Average, names_prefix = "T_") |> 
  rename(T_m5 = `T_-5`)
DFwide
#> # A tibble: 10 x 6
#>    Replica Bay   Boat  Depth  T_m5   T_0
#>      <int> <chr> <chr> <dbl> <dbl> <dbl>
#>  1       1 Vast  Small   1.5  2.64 2.44 
#>  2       2 Vast  Small   1.5  1.54 2.16 
#>  3       3 Vast  Small   1.5  2.84 2.01 
#>  4       4 Vast  Small   1.5  1.65 1.13 
#>  5       5 Vast  Small   1.5  2.76 2.10 
#>  6       6 Vast  Small   1.5  2.32 2.19 
#>  7       7 Vast  Small   1.5  1.47 2.55 
#>  8       8 Vast  Small   1.5  2.00 0.158
#>  9       9 Vast  Small   1.5  2.91 2.10 
#> 10      10 Vast  Small   1.5  2.32 1.79
t.test(x = DFwide$T_m5, y = DFwide$T_0, paired = TRUE)
#> 
#>  Paired t-test
#> 
#> data:  DFwide$T_m5 and DFwide$T_0
#> t = 1.502, df = 9, p-value = 0.1674
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#>  -0.1937574  0.9593993
#> sample estimates:
#> mean of the differences 
#>               0.3828209

Created on 2022-07-04 by the reprex package (v2.0.1)

The problem with your use of t.test(),

t.test(Dist.T.10.S.V.R1, x = "Time", y = "Average", paired = TRUE, alternative = "two.sided")

was that you passed your data frame name, Dist.T.10.S.V.R1, as an unnamed argument. It got assigned to the mu argument, which is the expected difference in averages when you run a paired test. Here is the Help file description of the t.test function

t.test(x, y = NULL,
       alternative = c("two.sided", "less", "greater"),
       mu = 0, paired = FALSE, var.equal = FALSE,
       conf.level = 0.95, ...)

You passed it x, y, paired, alternative, and the unnamed argument. Among the possible arguments, mu is the first one you have not explicitly named, so t.test() tried to use Dist.T.10.S.V.R1 as mu.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.