Statistics with R (3) - Generalized, linear, and generalized least squares models (LM, GLM, GLS)

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ธ.ค. 2024
  • In this video, I show how how to implement linear models, generalized linear models and generalized least squares models in R. Using the "airquality" dataset, I show how to fit and interpret the models. The core of the session is the interpretation of partial slope coefficients in Poisson generalized linear models. Finally, I give an outlook on generalized additive models which will be covered in one of the next sessions.
    The R code used in this video is:
    airquality
    plot(Ozone~Wind,airquality)
    model1=lm(Ozone~Wind,airquality)
    plot(model1)
    coef(model1)
    (Intercept) Wind
    96.872895 -5.550923
    #predictions for Wind speeds of 19 and 20 mph:
    Ozone1=coef(model1)[1]+coef(model1)[2]*19
    Ozone2=coef(model1)[1]+coef(model1)[2]*20
    Ozone1
    Ozone2
    ##
    model2=glm(Ozone~Wind,airquality,family=poisson)
    coef(model2)
    (Intercept) Wind
    5.0795877 -0.1488753
    Ozone1.glm=exp(coef(model2)[1]+coef(model2)[2]*19)
    Ozone2.glm=exp(coef(model2)[1]+coef(model2)[2]*20)
    Ozone2.glm/Ozone1.glm
    0.8616765
    exp(coef(model2)[2]) #exp(-0.1488753 )
    #0.8616765
    ##
    library(nlme)
    model3=gls(Ozone~Wind,airquality)
    summary(airquality$Ozone)
    model3=gls(Ozone~Wind,airquality,na.action=na.exclude)
    head(airquality)
    airquality$Date=
    as.Date(paste(1973,airquality$Month,airquality$Day,sep="-"))
    library(lattice)
    xyplot(Ozone~Date,airquality)
    model4=gls(Ozone~Wind*Date,airquality,na.action=na.exclude)
    air2=subset(airquality,complete.cases(Ozone))
    model5=gls(Ozone~Wind*Date,air2)
    plot(ACF(model5,form=~Date),alpha=0.05)
    model6=update(model5,correlation=corAR1())
    library(MuMIn)
    AICc(model5,model6)
    df AICc
    model5 5 1099.40
    model6 6 1095.21
    summary(model6)

ความคิดเห็น • 95