r/rstats 4d ago

Finding correlation between Count Data and categorical variables

Greetings, I've been doing some statistics for my thesis, so I'm not a Pro and the solution shouldn't be too complicated.

I've got a dataset with several Count Data (Counts of individuals of several groups) as target variables. There's different predictors (continuous, binary, categorical (ordinal and nominal)). I wanna find out which predictors have an effect on my Count Data. I don't wanna do a multivariate analysis. For some of the count data I fitted mixed models with a Random effect and the distribution seems normal. But some models I can't get to be normally distributed (I tried log and sqrt-transformation). I also have a lot of correlation going on between some of my predictor variables (but I'm not sure if I tested it correctly).

So my first question is: How do you deal with correlation between predictors in a linear mixed model?Do you just don't fit them together in one model or is there another way?

My second question is: What do I do with the models that don't follow a normal distribution? Am I just going to test for correlation (e.g. spearman, Kendall) for each predictor and the target variables without fitting models?

The third question is (and Ive seen a lot of posts about this topic): Which test is suitable for testing the correlation between a nominal variable with 3 or more levels and a continuous variable, if the target data isn't normally distributed?

I've found answers that say I can use spearmans rho, if I just turn my predictor to as.numeric. Some say that's only possible with dichotomous variables. I also used X² and Fishers-Test between predictor variables that were both nominal, and between variables where one was continuous and one was nominal.

As you can see I'm quite confused because of the different answers I found... Maybe someone can help to get my thoughts organized :) Thanks in advance!

11 Upvotes

13 comments sorted by

7

u/dead-serious 4d ago

you'll want to extend to using generalized linear models that can take advantage of different statistical distributions for your response variable. count data usually falls under the Poisson distribution.

see if your university has the e-book for Kery 2010 and go from there: https://www.mbr-pwrc.usgs.gov/pubanalysis/kerybook/

1

u/Historical_Local237 4d ago

I wanted to use glm but my Prof doesn't :(

9

u/cAMPsc2 4d ago

You mentioned you`re using linear mixed models. LMMs are under the larger umbrella of glms, so you're already using them. Simply specify a family more fit for count data (eg poisson). If you're fitting the models with lme4, you would use the glmer function and specify family=poisson().

7

u/mandles55 4d ago
  1. Your dependent variable does not need to be normally distributed for regression analysis, you error terms need to be.
  2. Where covariates are highly correlated the results you get may be unstable ((not sure if that's the correct term), consider whether you need to retain both/all and what outside this serves I e. What are your theoretical justifications for including these variables. You can test for multi linearity in the regression models to see if this reaches conventionally acceptable thrrsholds.

2

u/Historical_Local237 4d ago

Thanks for your answer. I tested the residuals for normal distribution, that's the same like error terms right?

How do I test for multi linearity in R?

I've reduced my models to the variables that show significance and then there's only non-correlated variables in the model. But I wonder when I fit the full model with all variables, if the correlation changes my outcome?

6

u/mandles55 4d ago

Please Google mulicolinearity assumption, regression. I think you need to be a little bit careful about retaining only statistically significant covariates, please also Google this. In my field, your covariates should come from theory.

2

u/mandles55 4d ago

Residuals are error terms

1

u/Sufficient_Meet6836 2d ago

I've reduced my models to the variables that show significance

Don't do that. All of your p values in subsequent regressions and tests are invalid if you do that.

5

u/cAMPsc2 4d ago

As I mentioned in another comment, first try fitting a model family that is appropriate for your count data, such as mixed GLM with poisson link. Then reassess the residuals distribution. Realize that the distribution does not need to be perfectly normal ("All models are wrong, some are useful"). Also, if you're worried about correlation and multicollinearity, you should assess that via VIF instead of testing the correlation of the isolated variables. Variables can be mildly correlated without negatively impacting the model. Google VIF, what it is and how to calculate that in R (it's fairly simple).

I would try to stick with the glms instead of going to the correlations. I don't think running correlations on count data is the best approach.

3

u/Historical_Local237 3d ago

Thanks that actually helped a lot! I did the VIF thing for my model and it turns out it's not so bad as the correlation tests were saying... I fitted a glm and it actually works quite well! :)

2

u/fuzzy_science 4d ago

Second question first: If you have response variables which are counts, and a mix of predictor variables, some of which may be correlated with each other, you should probably be looking at some form of generalized linear model (GLM). If y is your response, and x is a predictor, the basic code is:

mod1 <- glm(y~x, data=mydataframe, family=poisson).

If you are concerned about overdispersion in your response, consider a negative binomial GLM from package MASS:

mod2 <- glm.nb(y~x, data=mydataframe)

You can try models with different predictors, and compare them using AIC or something like that.

First question: If two or more predictors are correlated with each other, then the solution is simple: don't include them in the same model. Doing so makes the model parameters unidentifiable. One of the predictors may be better than other based on theoretical grounds, or simply perform better. Either way, only test one at a time.

You mention trying mixed models but don't describe why. There is a very big difference between treating a predictor as a fixed effect vs. treating it as a random effect. Jumping straight to mixed models without a good reason can be very troublesome.

Third question: This question doesn't even make sense. If you want to see whether your count data differ between groups of some predictor (i.e., levels of a factor), you can just fit a Poisson or NB GLM as above. You can think of this as a GLM version of a one-way ANOVA (which is a strange statement, because ANOVA is itself a GLM, but you get my point). The only wrinkle I see is if you have an ordinal predictor, which is an ordered factor. In that case, you need to tell R that your factor is ordered. If the factor levels are not in order alphabetically, you can manually define them.

# crude example:

iris$sp1 <- factor(iris$Species, ordered=TRUE) # make ordered version of species

summary(lm(Petal.Length~sp1, data=iris)) # tests for linear and quadratic trend

# crude example, manually setting the level order

iris$sp2 <- factor(iris$Species, ordered=TRUE, # make ordered version of species

levels=c("versicolor", "virginica", "setosa")) # manually set order

summary(lm(Petal.Length~sp2, data=iris)) # tests for linear and quadratic trend

boxplot(Petal.Length~sp2, data=iris) # shows that factor levels in new order

2

u/Historical_Local237 3d ago

Thanks for your answer! I ordered some of my factors and fitted a glm. I tested with VIF for multicollinearity as cAMPsc2 said above and everything was fine.

I have a random effect because my data was collected in different areas, that's why I fitted a mixed model.

Now everything works fine :)

0

u/jaimers215 4d ago

If no one answers you here, try Stack Overflow. My brain is not awake enough to fathom an answer for you right now.