Advertisement
Guest User

Plottar besök per årskull + Poissonmodeller

a guest
Apr 4th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.06 KB | None | 0 0
  1. install.packages("pscl")
  2. library(dplyr)
  3. library(MASS)
  4. library(pscl)
  5.  
  6. Besokarskull <- Beso_k_per_a_rskull_Anpassad_fo_r_R_och_SAS
  7. plot(Besokarskull$Ålder,Besokarskull$tot_total_tot)
  8. BesokUV <- filter(Besokarskull, Kommun=="Upplands-Väsby")
  9. BesokOST <- filter(Besokarskull, Kommun=="Östersund")
  10. BesokTOM <- filter(Besokarskull, Kommun=="Tomelilla")
  11. BesokMLM <- filter(Besokarskull, Kommun=="Malmö")
  12.  
  13. plot(BesokMLM$Ålder, BesokMLM$tot_total_tot, type ="n")
  14. lines(BesokMLM$Ålder, BesokMLM$tot_total_tot, col="black")
  15. lines(BesokUV$Ålder, BesokUV$tot_total_tot, col="blue")
  16. lines(BesokOST$Ålder, BesokOST$tot_total_tot, col="green")
  17. lines(BesokTOM$Ålder, BesokTOM$tot_total_tot, col="red")
  18.  
  19. plot(BesokUV$Ålder, BesokUV$tot_total_tot, type ="n")
  20. lines(BesokUV$Ålder, BesokUV$tot_total_tot, col="blue")
  21. lines(BesokOST$Ålder, BesokOST$tot_total_tot, col="green")
  22. lines(BesokTOM$Ålder, BesokTOM$tot_total_tot, col="red")
  23.  
  24. plot(BesokTOM$Ålder, BesokTOM$tot_total_tot, type ="n")
  25. lines(BesokTOM$Ålder, BesokTOM$tot_total_tot, col="red")
  26.  
  27. #Import and rename dataframe.
  28. Poissonbesok <- Sammansta_llning_2  
  29. #Creating expected count to use as offset
  30. Poissonbesok$E <- Poissonbesok$`Befolkning 2019` * (sum(Poissonbesok$`total 2019`)/sum(Poissonbesok$`Befolkning 2019`))
  31. Poissonbesok$ptatortsgrad <- Poissonbesok$`Tätortsgrad, 2019`/100
  32. #Poisson model
  33. fit.poi.besok<-glm(Poissonbesok$`total 2019`~offset(log(Poissonbesok$E))+scale(Poissonbesok$`Andel inv 65+, 2019`)+
  34. scale(Poissonbesok$`Andel inv 0-18 år, 2019`)+scale(Poissonbesok$ptatortsgrad),
  35. family=poisson, data=Poissonbesok)
  36. summary(fit.poi.besok)
  37. #Negative Binomial.
  38. fit.nb.besok<-glm.nb(Poissonbesok$`total 2019`~offset(log(Poissonbesok$E))+scale(Poissonbesok$`Andel inv 65+, 2019`)+
  39.                      scale(Poissonbesok$`Andel inv 0-18 år, 2019`)+scale(Poissonbesok$ptatortsgrad), data=Poissonbesok)
  40. summary(fit.nb.besok)
  41.  
  42. #Testing model fit.
  43. pchisq(fit.nb.besok$deviance, df = fit.nb.besok$df.residual)
  44.  
  45. hist(fitted(fit.nb.besok)/Poissonbesok$E,main="Sweden SMR Distribution from Negative Binomial Model" )
  46. hist(Poissonbesok$`total 2019`/Poissonbesok$E)
  47.  
  48. hist(fitted(fit.poi.besok)/Poissonbesok$E,main="Sweden SMR Distribution from Poisson Model" )
  49. #QuasiPoisson.
  50. fit.quasi.besok<-glm(Poissonbesok$`total 2019`~offset(log(Poissonbesok$E))+scale(Poissonbesok$`Andel inv 65+, 2019`)+
  51.                        scale(Poissonbesok$`Andel inv 0-18 år, 2019`)+scale(Poissonbesok$ptatortsgrad),
  52.                      family=quasipoisson, data=Poissonbesok)
  53. summary(fit.quasi.besok)
  54. #Negative binomial with befolkning(n) as off set.
  55. fit.nb.besok2<-glm.nb(Poissonbesok$`total 2019`~offset(log(Poissonbesok$`Befolkning 2019`))+scale(Poissonbesok$`Andel inv 65+, 2019`)+
  56.                        scale(Poissonbesok$`Andel inv 0-18 år, 2019`)+ scale(Poissonbesok$`Mediannettoinkomst, kr/inv 20+, 2018`)+ factor(Poissonbesok$`Kommunindelning i tre typer, Namn`), data=Poissonbesok)
  57.  
  58. summary(fit.nb.besok2)
  59.  
  60. #Test fit Neg Binomial vs.Poisson, this is also a test of equidispersion.  
  61. odTest(fit.nb.besok)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement