Periodic GLM or GLMM
Let take an example:
# generate data
jo <- 1:800
trans_trigo <- 10+sin((jo/365.25)*2*pi)*1+ cos((jo/365.25)*2*pi)*5
obs <- rnorm(n=800, mean=trans_trigo, sd=2)
# plot the data
plot(jo, obs, bty="n", xlim=c(1,800), ylim=c(0,20))
par(new=TRUE)
plot(jo, trans_trigo, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="",
axes=FALSE, col="violet", type="l", lwd=2)
# group is per month observation
mydat <- data.frame(obs=obs, jour=jo, jour2=jo*jo, group=round(jo/27))
# glm with ordinal day as linear factor
mod <- glm(obs ~ jour, data=mydat, family=gaussian)
summary(mod)
newdata <- data.frame(jour=1:800)
preddf2 <- predict(mod, type="response", newdata=newdata, se.fit=FALSE)
par(new=TRUE)
plot(jo, preddf2, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="", axes=FALSE, col="red", type="l")
# glm with ordinal day as second degree polynom
mod3 <- glm(obs ~ jour+jour2, data=mydat, family=gaussian)
summary(mod3)
newdata <- data.frame(jour=1:800, jour2=(1:800)^2)
preddf2 <- predict(mod3, type="response", newdata=newdata, se.fit=FALSE)
par(new=TRUE)
plot(jo, preddf2, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="", axes=FALSE, col="green", type="l")
# glm with ordinal day transformed as period factor
mod2 <- glm(obs ~ sin((jour/365.25)*2*pi)+ cos((jour/365.25)*2*pi), data=mydat, family=gaussian)
summary(mod2)
newdata <- data.frame(jour=1:800)
preddf2 <- predict(mod2, type="response", newdata=newdata, se.fit=FALSE)
par(new=TRUE)
plot(jo, preddf2, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="", axes=FALSE, col="blue", type="l")
# Same with glmm
library("MASS")
mod4 <- glmmPQL(obs ~ sin((jour/365.25)*2*3.141593)+ cos((jour/365.25)*2*3.141593), random= ~ 1 | group, data=mydat, family=gaussian)
summary(mod4)
newdata <- data.frame(jour=1:800, group=round(jo/27))
preddf2 <- predict(mod4, type="response", newdata=newdata, se.fit=FALSE)
par(new=TRUE)
plot(jo, preddf2, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="", axes=FALSE, col="yellow", type="l")
# generate data
jo <- 1:800
trans_trigo <- 10+sin((jo/365.25)*2*pi)*1+ cos((jo/365.25)*2*pi)*5
obs <- rnorm(n=800, mean=trans_trigo, sd=2)
# plot the data
plot(jo, obs, bty="n", xlim=c(1,800), ylim=c(0,20))
par(new=TRUE)
plot(jo, trans_trigo, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="",
axes=FALSE, col="violet", type="l", lwd=2)
# group is per month observation
mydat <- data.frame(obs=obs, jour=jo, jour2=jo*jo, group=round(jo/27))
# glm with ordinal day as linear factor
mod <- glm(obs ~ jour, data=mydat, family=gaussian)
summary(mod)
newdata <- data.frame(jour=1:800)
preddf2 <- predict(mod, type="response", newdata=newdata, se.fit=FALSE)
par(new=TRUE)
plot(jo, preddf2, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="", axes=FALSE, col="red", type="l")
# glm with ordinal day as second degree polynom
mod3 <- glm(obs ~ jour+jour2, data=mydat, family=gaussian)
summary(mod3)
newdata <- data.frame(jour=1:800, jour2=(1:800)^2)
preddf2 <- predict(mod3, type="response", newdata=newdata, se.fit=FALSE)
par(new=TRUE)
plot(jo, preddf2, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="", axes=FALSE, col="green", type="l")
# glm with ordinal day transformed as period factor
mod2 <- glm(obs ~ sin((jour/365.25)*2*pi)+ cos((jour/365.25)*2*pi), data=mydat, family=gaussian)
summary(mod2)
newdata <- data.frame(jour=1:800)
preddf2 <- predict(mod2, type="response", newdata=newdata, se.fit=FALSE)
par(new=TRUE)
plot(jo, preddf2, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="", axes=FALSE, col="blue", type="l")
# Same with glmm
library("MASS")
mod4 <- glmmPQL(obs ~ sin((jour/365.25)*2*3.141593)+ cos((jour/365.25)*2*3.141593), random= ~ 1 | group, data=mydat, family=gaussian)
summary(mod4)
newdata <- data.frame(jour=1:800, group=round(jo/27))
preddf2 <- predict(mod4, type="response", newdata=newdata, se.fit=FALSE)
par(new=TRUE)
plot(jo, preddf2, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="", axes=FALSE, col="yellow", type="l")
Commentaires
Enregistrer un commentaire