# How to tell if one value is different from another?

I have a dataframe made up of counts in different time periods as so:

> data
Time  X  Y
1    A 14 18
2    B 40 32
3    C 24 30
4    D 29 30
5    E 39 32
> dput(data)
structure(list(Time = c("A", "B", "C", "D", "E"), X = c(14, 40,
24, 29, 39), Y = c(18, 32, 30, 30, 32)), class = "data.frame", row.names = c(NA,
-5L))


I want to know if the pattern of change in X is the same as in Y over time, i.e. do significant changes between counts in X over time occur at the same point as significant changes in Y over time.

I know that I can do chi-squared tests to test if the difference between values is significant under the null hypothesis that there is no change, i.e., the observed and expected values are the same. For example, to test if the counts for X between time A and time B are different, I could do something like this:

1. Rearrange dataset
> X
Time Expected Observed
1  A-B       14       40
2  B-C       40       24
3  C-D       24       29
4  D-E       29       39
> dput(X)
structure(list(Time = c("A-B", "B-C", "C-D", "D-E"), Expected = c(14,
40, 24, 29), Observed = c(40, 24, 29, 39)), class = "data.frame", row.names = c(NA,
-4L))

1. Calculate degrees of freedom:
df <- (nrow(X)-1)*(2-1) #there are only 2 columns of data while the dataframe has 3

1. Run chi-squared test
1-pchisq(abs(14 - 40), df)


But I'm not sure if this is actually an appropriate thing to do.

Any thoughts or help on this matter would be appreciated!

Many thanks,

Carolina

You are right about using the chi-square but taking the difference between rows might not be necessary. So to answer your first question:

I want to know if the pattern of change in X is the same as in Y over time, i.e. do significant changes between counts in X over time occur at the same point as significant changes in Y over time.

You have only 1 data point, so you cannot tell if it is a significant change, or whatever significant change might mean.

What you can test is whether the row variables are independent of the column variable. If the row probabilities are the same across columns, then the difference between rows will be preserved as well. For example, we use the row probability of your first column to simulate data, you can see though count numbers are different, the trend is conserved:

set.seed(111)
p=data[,2]/sum(data[,2])
sim = rbinom(5,size=200,p=p)
plot(1:5,data[,2],type="b",ylim=c(0,50))
points(1:5,sim,type="b",col="orange")
legend("topleft",c("X","simulated"),fill=c("black","orange"))


We test this simulated data with a chi-sq and it is not significant:

chisq.test(cbind(data[,2],sim))

Pearson's Chi-squared test

data:  cbind(data[, 2], sim)
X-squared = 0.34147, df = 4, p-value = 0.987


So we apply the chi-sq test onto your dataset and check:

chisq.test(data[,2:3])

Pearson's Chi-squared test

data:  data[, 2:3]
X-squared = 2.7076, df = 4, p-value = 0.6079


We cannot reject the hypothesis that the row variable is independent of the column. You can roughly say there is insufficient evidence to suggest the patterns are different.