# Repeated measures regression or time series analysis?

I have managed to confuse myself again, so I hope you could give me some tips or point into the right direction.

I have pesticide residue data in different plant matrices (e.g. flowers, leaves) from different experiments. Samples were taken on 5 subsequent days (DAA). Each day five repetitions were collected (from the same plants- repeated measures). I also have additional information like temperature or the applied dose rate which could affect the residues.

    A tibble: 6 x 9
# Groups:   DAA, App [6]
Repetition   DAA Doserate AI     App          Site  Temperature Flowers Leaves
<dbl> <dbl>    <dbl> <chr>  <chr>        <chr>       <dbl>   <dbl>  <dbl>
1          1     0     0.5  Fludio Field_Fludio Field        16.9   22.3    80.6
2          1     0     0.75 Cyp    Field_Cypro  Field        16.9   31.0    52.7
3          1     0     0.75 Cyp    Hand         GH           20.5   89.6   306.
4          1     1     0.5  Fludio Field_Fludio Field        16.8    3.24   66.7
5          1     1     0.75 Cyp    Field_Cypro  Field        16.8    7.36   56.3
6          1     1     0.75 Cyp    Track        GH           20.5    5.42  280.


I would like to run a regression and see whether I could predict residues in flowers from residues I found in leaves. From graphs I looked at I assume the sampling day (DAA) and temperature would affect the residues as well and could be included into the model. I thought I could do something like this: fit1 <- lmer(Pollen ~ Flowers*DAA+Temperature+(1|Repetition), data=apptest) So far (in other analyses with lmer) I have used the sampling days (DAA) as a factor variable. But now I have read that I cant use a factor in regression. So either I would have to use DAA as numeric variable or instead I would have to introduce dummy variables. I think I am confused about whether DAA is a factor or a numeric variable. From this plot it looks like a factor:

It would be ok to have 4 dummy variables for this data set, but I have another dataset with 12 sampling days. I also came across time series analysis to make predictions over time, but I dont think that would make any sense with only 5 days? So my questions: Is my variable day(DAA) a factor variable or a numeric one? How would I include it into my regression model? And in which circumstances/for which kind of analysis is it ok to have a factor in a lmer model?

Many thanks for your help in advance!

• If the residues you found in leaves are equally affected by time as those you found in the flowers, regressing on residues could be enough to take the time effect into account. Else, you would need to model the time as well. You could check graphically if you have some remaining time trend by plotting the residuals against DAA. If you are not directly interested in the effect of time, that is. DAA should be seen as a time index, not a factor.
– wiwh
Commented Nov 13, 2020 at 12:53
• Thank you very much for your advice, some things are already much clearer. Leaves and flowers were equally affected by time in this data set, but it might be useful for other data sets. Furthermore, I have plotted the residuals against DAA, and there is some pattern visible, so I assume DAA could have an effect. What would be the best approach to include DAA into the regression? Is it sufficient to use as.ts(DAA) and just include it? I have found a lot of information about time series online, but mainly of huge data sets with years of data!
– Fee
Commented Nov 13, 2020 at 19:15

## 1 Answer

But now I have read that I cant use a factor in regression.

I don't know where you have read this but there must be some misunderstanding. You can, of course, use a factor variable in regression models.

So either I would have to use DAA as numeric variable or instead I would have to introduce dummy variables.

When you use a factor variable in a regression model, the software creates dummy variables behind the scenes.

Based on the information given, I don't see anything wrong with your model:

lmer(Pollen ~ Flowers*DAA+Temperature+(1|Repetition), data=apptest)


Since DDA is measuring time, it probably makes more sense to code is as numeric, especially when you have 12 time points, otherwise it will make the interpretation quite cumbersome. You may of course, also want to investigate nonlinearity.

• Thanks a lot! But to be honest, now I am even more confused... It would make my life much easier if I could just include factors (and tbh I didnt quite understand why I shouldnt), but I have been reading a lot (online) that the variables should be numeric or be converted into dummies...
– Fee
Commented Nov 13, 2020 at 19:20
• You can include factors, I didn't say you can't. I just said it would make the interpretation easier, if you code it numeric. I don't know why you think it would make your life easier ? Commented Nov 13, 2020 at 19:30
• Yes I understood your comment-sorry if i havent been quite clear. I just became so confused because I have read in several posts that I couldnt include factors in a regression to predict a variable, but that was what I had done so far (I will try to find these posts again and include the links). You have simply taken away all my confusion and confirmed that I have done everything correctly so far- thanks for that! Another question if you dont mind: Why does my Rsqrt decrease when I use DAA as numeric instead of as factor (from 0.85 to 0.74)?
– Fee
Commented Nov 13, 2020 at 19:51
• You're welcome. Again: you CAN include a categorical (factor) variable in a regression model. As for the question about $R^2$, this is a mixed effects model and $R^2$ is not well defined for mixed models, as it would be for a model without random effects. There are many proposals for ways to calculate a $R^2$-like quantity but none of these have the same properties as $R^2$ for a model without random effects. So the answer to your question will depend on how it is calculated. It might be related to degrees of freedom (the factor variable will use more). You can ask a new question about that :) Commented Nov 13, 2020 at 20:00
• Thanks! I have used r.squaredGLMM(x)  from library(MuMIn) for the calculation, just to get an idea of what is going on. I am sure I can find information there on how it is calculated and do some research myself and hope I wont confuse myself again :)
– Fee
Commented Nov 13, 2020 at 20:11