0

I've created a linear mixed effects model where the explanatory variable is mother age and response variable is partner age along with several random effects. I've had no problem creating the model but now that im using it to generate predicted values to fit to the graph I am having some difficulties! As far as i can tell the code is correct and I cannot tell why this isnt working.

First i tried the following

pred <- expand.grid(Mother.age=EPPs$Mother.age)
head(pred)
tail(pred) 
pred$fit <- predict(m6, newdata=EPPs, type='response', re.form=~0)
head(pred) #here i have predicted and fitted values but they are not in order so I tried the following

pred[order(pred$fit),] ##this said that it worked but changed nothing

plot(jitter(Genetic.father.age) ~ jitter(Mother.age), data=EPPs)

lines(fit ~ Mother.age, data=pred, col='red')

This essentially ends up in a massive back and forth red scribble instead of a line so i decided to manually order the data points in excel and try again

pred1 <- read.csv("predictions1.csv", header=TRUE) ##These are the ordered point
head(pred1)
tail(pred1)
pred$fit <- predict(m6, newdata=EPPs, type='response', re.form=~0)

plot(jitter(Genetic.father.age) ~ jitter(Mother.age), data=EPPs)

lines(fit ~ Mother.age, data=pred1, col='red')

Now this gets me very close to a straight line however it still has little 'step-ups' at each year on the x-axis, what I really want is a smooth flat line!

Any help would be appreciated - Im not sure what else I can do.

Oh whilst i'm here - any recommendations for a post hoc test for comparing two lme models to see if there is a significant difference? In this case it would be the exact same model except the response variable on the first is genetic father age and the second would be social father age (I want to show that extra pair males are consistently older than the cuckolded social males on average)

Thanks!!

Whole example

EPPs <- read.csv("EPPs.csv", header=TRUE) #data i am using

Turn these bad boys to factors to be used as random effects

EPPs$Mother <- as.factor(EPPs$Mother) 
EPPs$Mother.Cohort <- as.factor(EPPs$Mother.Cohort)
EPPs$Brood.year <- as.factor(EPPs$Brood.year)
EPPs$Social.Father <- as.factor(EPPs$Social.Father) 

Within subject centering

AveByInd <- function(x) mean(x)
d2 <- do.call("rbind", as.list(
  by(EPPs, EPPs["Mother"], transform, AveMAge=AveByInd(Mother.age))))
par(mfrow=c(1,1))
hist(d2$AveMAge, xlab="Average mother age", ylab="Frequency", main="")

WithinIndCentr <- function(x) x-mean(x)
d2 <- do.call("rbind", as.list(
  by(d2, d2["Mother"], transform, WithinMAge=WithinIndCentr(Mother.age))))
par(mfrow=c(1,1))
hist(d2$WithinMAge, xlab="Within-female centered age", ylab="Frequency", 
main
     ="")

m6<-lmer(d2$Genetic.father.age~d2$WithinMAge+d2$AveMAge+(1|Mother)+
(1|Mother.Cohort)+(1|Brood.year)+(1|Social.Father), data=d2)
summary(m6)

The model I have selected

Above this point everything is working fine Now I want to use the predictions generated by my model to add a line of best fit to my graph

I should mention I decided to try it with the Mother.age variable rather than the within-subject centered variables as I thought it would be more straight forward Originally I was trying this

pred <- expand.grid(Mother.age=EPPs$Mother.age)
pred$fit <- predict(m6, newdata=EPPs, type='response', re.form=~0)
head(pred)
pred[order(pred$fit),]

I was told I had to order the matrix in order for it to work however this doesnt seem to work

plot(jitter(Genetic.father.age) ~ jitter(Mother.age), data=EPPs)
lines(fit ~ Mother.age, data=pred, col='red')

This results in a horrible back and forth mess of red lines I was told by a friend that I needed to order the matrix to get it to work so I tried doing it manually in excel and reuploading

pred1 <- read.csv("predictions1.csv", header=TRUE) ##almost works
head(pred1)##predictions are in order

dev.off()
plot(jitter(Genetic.father.age) ~ jitter(Mother.age), data=EPPs) 

Using jitter because discrete data points and I want to give an idea of areas with lots of overlapping points

lines(fit ~ Mother.age, data=pred1, col='red')

Now I get a very almost straight line but it still has little 'steps' and I want it to be totally smooth

0 Answers0