2
$\begingroup$

I have a data frame which has two columns (Year and value).
The data is given below:
df

Year  Value
2016  0.0022
2016  0.0421
2016  0.0439
2016 0.07878
2016 0.00188
2016 0.00137
2016 0.00202
2017 0.24889
2017 0.44799
2017 0.13207
2017 0.03762
2017 0.04837
2017 0.12589
2017 0.02087
2018 0.0082
2018 0.01455
2018 0.01139
2018 0.03986
2018 0.00895
2018 0.00882

The data goes up to year 2020. My null hypothesis is that the means of all the years (i.e. from 2016 to 2020) are the same. I tried to calculate the t-test using R by the following code:

t.test(df$Value,df$Year,mu=0,paired=F)

When I looked at the result, one of the section of results mentioned:

sample estimates:
   mean of x    mean of y 
   0.1555973 2017.9402893 

I am bit puzzled by looking at the value of mean of y, it does not look intuitive. Is there anyone that can guide me whether I am following the right track or not? Sorry I have just started learning stats.

$\endgroup$

2 Answers 2

5
$\begingroup$

The test is specified wrongly. So you can only do t.test between two groups, for example between 2016 and 2017, and you use "~", where on the left hand side you have the response (Value) and right hand side, the variable (Year):

t.test(Value ~ Year,data=subset(df,Year %in% c(2016,2017)))

    Welch Two Sample t-test

data:  Value by Year
t = -2.1642, df = 6.4894, p-value = 0.07023
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.26813999  0.01401142
sample estimates:
mean in group 2016 mean in group 2017 
        0.02460714         0.15167143 

Since you have 3 groups, what you need is an anova, below you model Value as explained by different Years, each treated as a factor:

anova(lm(Value ~ factor(Year),data=df))
Analysis of Variance Table

Response: Value
             Df   Sum Sq  Mean Sq F value  Pr(>F)  
factor(Year)  2 0.078795 0.039398  4.6023 0.02527 *
Residuals    17 0.145526 0.008560                  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

This suggest there is a small difference in Years, and you can check using a pairwise.t.test:

pairwise.t.test(df$Value,df$Year,p.adj="BH")

    Pairwise comparisons using t tests with pooled SD 

data:  df$Value and df$Year 

     2016 2017
2017 0.03 -   
2018 0.86 0.03

P value adjustment method: BH 

Most likely 2017 is very different from the others. I suggest using the anova above since you have 5 groups (years).

$\endgroup$
2
$\begingroup$

I think you need to treat year as a continuous variable, not binary, as you would with a t-test, or a categorical variable, as with an ANOVA. My suggestion would be to use a linear regression as follows:

model <- lm(value ~ year, data = df)
summary(model)

Based on the data in your example, this gives you p = .972.

EDIT: However, this assumes a linear pattern in the data. StupidWolf finds 2017 stands out, which suggests the association is not linear. It may be useful to visualise the data to see what's happening. I've supplied some code to do this. Black line is a fitted linear pattern; blue line is a fitted LOESS patternThere is a clear spike in 2017. As StupidWolf suggests, ANOVA is most appropriate because the independent variable (year), if treated as a categorical variable, has more than two categories.

library(tidyverse)
library(janitor)


df <- tibble::tribble(
  ~Year, ~Value,
  "2016", "0.0022",
  "2016", "0.0421",
  "2016", "0.0439",
  "2016", "0.07878",
  "2016", "0.00188",
  "2016", "0.00137",
  "2016", "0.00202",
  "2017", "0.24889",
  "2017", "0.44799",
  "2017", "0.13207",
  "2017", "0.03762",
  "2017", "0.04837",
  "2017", "0.12589",
  "2017", "0.02087",
  "2018", "0.0082",
  "2018", "0.01455",
  "2018", "0.01139",
  "2018", "0.03986",
  "2018", "0.00895",
  "2018", "0.00882"
) %>% 
  clean_names() %>% 
  mutate(year = as.numeric(year), 
         value = as.numeric(value))

ggplot(df, aes(year, value)) + 
  geom_jitter(aes(colour = as.factor(year)), 
              width = .1) +
  geom_smooth(se = FALSE) + 
  geom_smooth(method = "lm", 
              colour = "black", 
              se = FALSE)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.