# Statistical test for logical vectors in R

The first issue that I feel like I have is that I have limited experience with actually using statistical tests. I apologize ahead of time if this question is illogical.

I have two example vectors below:

A = 0,0,0,0,1,1,0,0,1,0,1,0,0,0,1,1,0,0,1,0,0,1,0,1,0,1
B = 1,1,1,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0,1,1,0,1


1 representing a situation in which an event occurs and 0 representing a situation in which an event does not occur. What I know tells me "To find if there is a significant difference between the two groups, run a t-test". The problem that I see is that these numbers represent TRUE or FALSE values rather than quantifiable data, if that makes sense. These numbers could just as easily be 100 and 0 and the logic would technically hold. I don't know if there is a test that examines the difference in TRUE vs. FALSE occurrences between two groups, so I thought that I would ask here.

If it helps the post make sense, I have two types of plants in a plot that I examine weekly for insect eggs. If there are eggs, the value is 1. If there are no eggs, the value is 0. I am trying to see if there is a statistically significant difference between groups A and B in terms of whether or not eggs were laid.

## migrated from stackoverflow.comJul 19 '15 at 14:01

This question came from our site for professional and enthusiast programmers.

• Thank you. I was wondering if I should have posted it somewhere else. – Adam Jul 19 '15 at 5:28
• "t-tests" are for at least somewhat "normally" distributed data. If you want a test of concordance of paired binomial data you should be looking at a McNemar's test. For this data which is not paired, you really need to be more clear about what you mean by "examining the difference in TRUE vs. FALSE occurrences between two groups." If it is just an examination of the proportion of 1's, then look at prop.test. – DWin Jul 19 '15 at 6:26
• A t-test will work fine with these data. Have you noticed that its results do not depend on how you encode your data? Try it with the $100-0$ encoding you mention (in place of the $0-1$ encoding). – whuber Jul 19 '15 at 18:22
• Are you looking to test for a difference in the proportion of "TRUE" (which is the same thing as a difference in means)? – Glen_b Jul 20 '15 at 16:27

The question is, what exactly do you want to test? In short: you can use the Pearson's Chi squared test: prop.test() to test if the probability of success is the same in both samples. Here is the corresponding code for your data:

# Assign your data correctly:
A  <- c(0,0,0,0,1,1,0,0,1,0,1,0,0,0,1,1,0,0,1,0,0,1,0,1,0,1)
B  <- c(1,1,1,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0,1,1,0,1)

# Get number of successes for A:
suc.A <- sum(A)
# Number of trials in A:
trial.A <- length(A)

# same for B:
suc.B <- sum(B)
trial.B <- length(B)

# apply Pearson's Chi-squared test:
prop.test(x=c(suc.A, suc.B), n=c(trial.A, trial.B))

# Or in short:
prop.test(c(sum(A),sum(B)), c(length(A), length(B))


So the test says there is no significant difference in the probability of success, although the empirical difference is almost 20% -- false of too little data I suppose.

More general information: The data you describe is called "binary" as it has just two outcomes. This is also why the t-test is not appropriate. A typical distribution for binary data would be the binomial distribution. You can google it and you will find a lot of information about it. Note, that not all binary data is always binomially distributed. E.g. if your data has autorcorrelation (many zero's in a row, many ones in a row) or a trend towards zero or towards one, this would not be binomial because the probability of success is not constant.

In case you also have information of the joint distribution of A and B (how often is A a success when B is and vice-versa) you could test their relation using fisher exact test. See ?fisher.test

See also the following discussions: Comparing two binary distributions Test if two binomial distributions are statistically different from each other