R/gam.R

Defines functions denforW denforM

denforM <- function(templ3,rainl2,rainl3,month)
{

  require(mgcv)

  gam_train <- data.frame(dengue=(c(48,47,47,63,142,66,67,50,35,24,15,3,61,43,32,59,52,32,21,20,24,16,15,4,56,43,37,86,51,51,50,20,37,33,17,22,151,160,261,36,13,4,12,10,20,11,9,18,15,31,39,30,23,14,7,17,13,19,34,101,172,230,247,63,31,37,31,31,15,14,11,12,80,99,129,99,136,74,60,35,15,15,17,8,96,95,81,139,132,61,52,18,10,16,19,49,85,67,54,96,54,64,91,56,18,23,24,56,70,95,162,152,162,208,160,136,71,90,92,119)),templ3=(c(NA,NA,NA,25.4,26.0,26.1,26.9,27.2,26.3,25.6,25.6,27.4,27.1,27.0,26.3,26.5,26.2,27.1,27.1,27.2,26.2,25.9,25.1,26.2,27.5,27.8,27.2,26.6,26.4,27.0,27.8,26.9,26.3,24.8,25.3,26.7,27.1,NA,26.5,27.0,26.5,26.6,28.0,27.2,25.9,26.2,25.4,26.8,27.5,27.6,26.7,26.7,26.7,27.3,27.3,27.7,27.5,26.2,25.8,27.0,27.3,27.4,25.9,26.4,26.8,26.6,26.5,26.7,25.6,24.9,25.1,25.7,27.5,29.0,27.5,27.0,27.3,26.6,27.0,27.5,26.4,25.7,25.5,26.0,27.5,26.8,26.4,26.3,25.8,25.7,26.8,26.3,25.6,24.4,25.7,27.0,27.5,26.3,26.0,26.2,25.7,26.7,27.3,26.8,26.6,25.2,25.4,26.8,27.8,27.9,27.5,26.8,27.2,27.6,27.5,27.6,27.3,26.9,27.2,27.2)),rainl2=(c(NA,NA,493,304,466,335,46,83,30,0,2,212,201,161,530,456,166,120,97,0,1,0,0,2,262,236,174,465,233,44,123,14,0,0,0,29,NA,301,334,159,268,19,23,6,13,7,1,13,329,424,346,484,152,254,1,106,123,12,5,128,87,481,391,309,337,232,195,0,0,0,0,1,42,329,100,335,284,326,40,39,3,0,0,53,185,709,174,346,242,264,106,9,0,0,2,191,306,232,317,312,249,191,122,42,0,0,0,70,121,185,254,312,414,131,319,113,35,155,401,157)),rainl3=(c(NA,NA,NA,493,304,466,335,46,83,30,0,2,212,201,161,530,456,166,120,97,0,1,0,0,2,262,236,174,465,233,44,123,14,0,0,0,29,NA,301,334,159,268,19,23,6,13,7,1,13,329,424,346,484,152,254,1,106,123,12,5,128,87,481,391,309,337,232,195,0,0,0,0,1,42,329,100,335,284,326,40,39,3,0,0,53,185,709,174,346,242,264,106,9,0,0,2,191,306,232,317,312,249,191,122,42,0,0,0,70,121,185,254,312,414,131,319,113,35,155,401)),month=(c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12)))

  gam_train$dengue <- as.integer(gam_train$dengue)
  gam_train$templ3 <- as.numeric(gam_train$templ3)
  gam_train$rainl2 <- as.integer(gam_train$rainl2)
  gam_train$rainl3 <- as.integer(gam_train$rainl3)
  gam_train$month <- as.factor(gam_train$month)

  gam.mod <- gam(dengue ~ s(templ3,k=4) + s(rainl2,k=4) + s(rainl3,k=4) + month, family=quasipoisson, na.action=na.exclude, data=gam_train)

  gam_test <- data.frame(templ3,rainl2,rainl3,month)
  gam.prediction.m <- predict(gam.mod, type="response", newdata=as.data.frame(gam_test))
}

denforW <- function(rainl5,rainl7,rainl8,rainl9)
{

  require(mgcv)

  gam_trainW <- data.frame(OutMean=(c(0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,1,1,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,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0,2.7,0.0,0.0,0.0,0.0,0.0,7.0,0.0,0.8,0.0,0.6,0.0,0.0,0.0,11.6,1.0,4.6,14.9,3.8,219.1,240.2,7.0,27.4,142.1,93.6,29.0,31.8,227.2,45.6,15.7,145.2,167.5,152.3,48.3,31.3,2.1,15.8,144.2,71.9,89.9,14.0,4.0,1.0,0.0,0.0,0.0,0.0,0.0,38.3,62.3,5.3,120.4,2.6,0.0,0.0,0.0,0.0,12.2,0.0,0.0,0.9,3.4,0.8,0.0,0.0,9.9,114.6,3.9,53.5,0.0,4.8,24.4,56.8,97.4,154.8,82.1,104.3,18.9,50.7,73.3,212.7,91.5,73.3,67.2,75.1,70.3,6.2,194.9,61.1,71.7,49.2,102.6,38.2,0.4,68.5,17.9,23.9,58.3,26.1,0.0,0.0,0.0,0.0,0.0,0.3,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4,0.0,0.0,0.2,16.1,0.0,12.6,12.6,23.9,93.0,70.8,141.0,3.5,10.1,31.9,52.0,85.9,45.0,108.0,93.2,70.5,26.1,71.5,88.5,62.6,62.4,122.0,89.1,22.1,0.2,1.0,35.4,0.0,3.5,8.9,0.0,11.7,18.6,0.4,2.4,0.5,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.7,100.8,87.5,24.7,0.0,20.5,62.4,293.4,36.4,277.4,65.2,24.2,29.7,54.3,62.9,128.8,60.0,71.1,72.3,57.2,66.8,86.8,18.4,113.7,127.1,22.6,0.2,15.1,32.6,0.0,58.0,0.0,0.0,9.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.2,0.0,0.0,0.0,27.8,27.7,77.1,75.4,115.0,59.2,61.8,52.2,37.0,78.7,37.6,55.4,93.9,30.9,83.5,29.9,119.8,110.8,99.9,25.9,76.4,29.5,4.5,112.6,46.5,105.8,3.0,8.7,59.9,50.6,19.1,1.8,36.4,65.0,0.4,41.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.3,69.0,0.0,0.0,24.1,79.9,16.9,6.5,55.0,0.0,103.1,20.8,52.1,83.0,77.2,41.5,67.0,92.6,145.0,7.0,9.2,145.5,158.1,91.1,9.7,39.4,3.8,78.4,24.2,29.4,126.7,54.3,94.3,7.4,73.2,22.5,0.3,29.5,7.1,3.2,0.0,4.4,0.0,58.8,10.8,71.6,20.7,169.7,61.4,162.7,3.0,14.1,45.6,59.5,35.0,52.4,103.0,0.0)),
                          rainl7=(c(NA,NA,NA,NA,NA,NA,NA,70.2,12.2,1.1,44.8,48.5,127.3,79.0,64.2,197.8,101.3,42.2,56.4,28.2,17.1,23.4,0.0,2.2,82.5,41.4,0.0,0.0,0.0,0.9,0.0,12.5,0.6,0.4,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.4,1.0,0.0,5.0,NA,NA,NA,NA,NA,109.4,36.7,62.8,84.0,22.9,21.0,50.8,42.9,204.3,43.5,11.7,87.6,15.6,95.1,40.5,106.2,25.2,0.6,14.8,0.0,4.6,2.6,0.0,6.6,8.5,5.4,0.0,2.5,0.0,3.6,10.0,0.0,2.7,0.0,0.0,0.0,0.0,0.0,7.0,0.0,0.8,0.0,0.6,0.0,0.0,0.0,11.6,1.0,4.6,14.9,3.8,219.1,240.2,7.0,27.4,142.1,93.6,29.0,31.8,227.2,45.6,15.7,145.2,167.5,152.3,48.3,31.3,2.1,15.8,144.2,71.9,89.9,14.0,4.0,1.0,0.0,0.0,0.0,0.0,0.0,38.3,62.3,5.3,120.4,2.6,0.0,0.0,0.0,0.0,12.2,0.0,0.0,0.9,3.4,0.8,0.0,0.0,9.9,114.6,3.9,53.5,0.0,4.8,24.4,56.8,97.4,154.8,82.1,104.3,18.9,50.7,73.3,212.7,91.5,73.3,67.2,75.1,70.3,6.2,194.9,61.1,71.7,49.2,102.6,38.2,0.4,68.5,17.9,23.9,58.3,26.1,0.0,0.0,0.0,0.0,0.0,0.3,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4,0.0,0.0,0.2,16.1,0.0,12.6,12.6,23.9,93.0,70.8,141.0,3.5,10.1,31.9,52.0,85.9,45.0,108.0,93.2,70.5,26.1,71.5,88.5,62.6,62.4,122.0,89.1,22.1,0.2,1.0,35.4,0.0,3.5,8.9,0.0,11.7,18.6,0.4,2.4,0.5,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.7,100.8,87.5,24.7,0.0,20.5,62.4,293.4,36.4,277.4,65.2,24.2,29.7,54.3,62.9,128.8,60.0,71.1,72.3,57.2,66.8,86.8,18.4,113.7,127.1,22.6,0.2,15.1,32.6,0.0,58.0,0.0,0.0,9.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.2,0.0,0.0,0.0,27.8,27.7,77.1,75.4,115.0,59.2,61.8,52.2,37.0,78.7,37.6,55.4,93.9,30.9,83.5,29.9,119.8,110.8,99.9,25.9,76.4,29.5,4.5,112.6,46.5,105.8,3.0,8.7,59.9,50.6,19.1,1.8,36.4,65.0,0.4,41.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.3,69.0,0.0,0.0,24.1,79.9,16.9,6.5,55.0,0.0,103.1,20.8,52.1,83.0,77.2,41.5,67.0,92.6,145.0,7.0,9.2,145.5,158.1,91.1,9.7,39.4,3.8,78.4,24.2,29.4,126.7,54.3,94.3,7.4,73.2,22.5,0.3,29.5,7.1,3.2,0.0,4.4,0.0,58.8,10.8,71.6,20.7,169.7,61.4,162.7,3.0,14.1,45.6,59.5,35.0,52.4)),
                          rainl8=(c(NA,NA,NA,NA,NA,NA,NA,NA,70.2,12.2,1.1,44.8,48.5,127.3,79.0,64.2,197.8,101.3,42.2,56.4,28.2,17.1,23.4,0.0,2.2,82.5,41.4,0.0,0.0,0.0,0.9,0.0,12.5,0.6,0.4,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.4,1.0,0.0,5.0,NA,NA,NA,NA,NA,109.4,36.7,62.8,84.0,22.9,21.0,50.8,42.9,204.3,43.5,11.7,87.6,15.6,95.1,40.5,106.2,25.2,0.6,14.8,0.0,4.6,2.6,0.0,6.6,8.5,5.4,0.0,2.5,0.0,3.6,10.0,0.0,2.7,0.0,0.0,0.0,0.0,0.0,7.0,0.0,0.8,0.0,0.6,0.0,0.0,0.0,11.6,1.0,4.6,14.9,3.8,219.1,240.2,7.0,27.4,142.1,93.6,29.0,31.8,227.2,45.6,15.7,145.2,167.5,152.3,48.3,31.3,2.1,15.8,144.2,71.9,89.9,14.0,4.0,1.0,0.0,0.0,0.0,0.0,0.0,38.3,62.3,5.3,120.4,2.6,0.0,0.0,0.0,0.0,12.2,0.0,0.0,0.9,3.4,0.8,0.0,0.0,9.9,114.6,3.9,53.5,0.0,4.8,24.4,56.8,97.4,154.8,82.1,104.3,18.9,50.7,73.3,212.7,91.5,73.3,67.2,75.1,70.3,6.2,194.9,61.1,71.7,49.2,102.6,38.2,0.4,68.5,17.9,23.9,58.3,26.1,0.0,0.0,0.0,0.0,0.0,0.3,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4,0.0,0.0,0.2,16.1,0.0,12.6,12.6,23.9,93.0,70.8,141.0,3.5,10.1,31.9,52.0,85.9,45.0,108.0,93.2,70.5,26.1,71.5,88.5,62.6,62.4,122.0,89.1,22.1,0.2,1.0,35.4,0.0,3.5,8.9,0.0,11.7,18.6,0.4,2.4,0.5,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.7,100.8,87.5,24.7,0.0,20.5,62.4,293.4,36.4,277.4,65.2,24.2,29.7,54.3,62.9,128.8,60.0,71.1,72.3,57.2,66.8,86.8,18.4,113.7,127.1,22.6,0.2,15.1,32.6,0.0,58.0,0.0,0.0,9.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.2,0.0,0.0,0.0,27.8,27.7,77.1,75.4,115.0,59.2,61.8,52.2,37.0,78.7,37.6,55.4,93.9,30.9,83.5,29.9,119.8,110.8,99.9,25.9,76.4,29.5,4.5,112.6,46.5,105.8,3.0,8.7,59.9,50.6,19.1,1.8,36.4,65.0,0.4,41.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.3,69.0,0.0,0.0,24.1,79.9,16.9,6.5,55.0,0.0,103.1,20.8,52.1,83.0,77.2,41.5,67.0,92.6,145.0,7.0,9.2,145.5,158.1,91.1,9.7,39.4,3.8,78.4,24.2,29.4,126.7,54.3,94.3,7.4,73.2,22.5,0.3,29.5,7.1,3.2,0.0,4.4,0.0,58.8,10.8,71.6,20.7,169.7,61.4,162.7,3.0,14.1,45.6,59.5,35.0)),
                          rainl9=(c(NA,NA,NA,NA,NA,NA,NA,NA,NA,70.2,12.2,1.1,44.8,48.5,127.3,79.0,64.2,197.8,101.3,42.2,56.4,28.2,17.1,23.4,0.0,2.2,82.5,41.4,0.0,0.0,0.0,0.9,0.0,12.5,0.6,0.4,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.4,1.0,0.0,5.0,NA,NA,NA,NA,NA,109.4,36.7,62.8,84.0,22.9,21.0,50.8,42.9,204.3,43.5,11.7,87.6,15.6,95.1,40.5,106.2,25.2,0.6,14.8,0.0,4.6,2.6,0.0,6.6,8.5,5.4,0.0,2.5,0.0,3.6,10.0,0.0,2.7,0.0,0.0,0.0,0.0,0.0,7.0,0.0,0.8,0.0,0.6,0.0,0.0,0.0,11.6,1.0,4.6,14.9,3.8,219.1,240.2,7.0,27.4,142.1,93.6,29.0,31.8,227.2,45.6,15.7,145.2,167.5,152.3,48.3,31.3,2.1,15.8,144.2,71.9,89.9,14.0,4.0,1.0,0.0,0.0,0.0,0.0,0.0,38.3,62.3,5.3,120.4,2.6,0.0,0.0,0.0,0.0,12.2,0.0,0.0,0.9,3.4,0.8,0.0,0.0,9.9,114.6,3.9,53.5,0.0,4.8,24.4,56.8,97.4,154.8,82.1,104.3,18.9,50.7,73.3,212.7,91.5,73.3,67.2,75.1,70.3,6.2,194.9,61.1,71.7,49.2,102.6,38.2,0.4,68.5,17.9,23.9,58.3,26.1,0.0,0.0,0.0,0.0,0.0,0.3,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4,0.0,0.0,0.2,16.1,0.0,12.6,12.6,23.9,93.0,70.8,141.0,3.5,10.1,31.9,52.0,85.9,45.0,108.0,93.2,70.5,26.1,71.5,88.5,62.6,62.4,122.0,89.1,22.1,0.2,1.0,35.4,0.0,3.5,8.9,0.0,11.7,18.6,0.4,2.4,0.5,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.7,100.8,87.5,24.7,0.0,20.5,62.4,293.4,36.4,277.4,65.2,24.2,29.7,54.3,62.9,128.8,60.0,71.1,72.3,57.2,66.8,86.8,18.4,113.7,127.1,22.6,0.2,15.1,32.6,0.0,58.0,0.0,0.0,9.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.2,0.0,0.0,0.0,27.8,27.7,77.1,75.4,115.0,59.2,61.8,52.2,37.0,78.7,37.6,55.4,93.9,30.9,83.5,29.9,119.8,110.8,99.9,25.9,76.4,29.5,4.5,112.6,46.5,105.8,3.0,8.7,59.9,50.6,19.1,1.8,36.4,65.0,0.4,41.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.3,69.0,0.0,0.0,24.1,79.9,16.9,6.5,55.0,0.0,103.1,20.8,52.1,83.0,77.2,41.5,67.0,92.6,145.0,7.0,9.2,145.5,158.1,91.1,9.7,39.4,3.8,78.4,24.2,29.4,126.7,54.3,94.3,7.4,73.2,22.5,0.3,29.5,7.1,3.2,0.0,4.4,0.0,58.8,10.8,71.6,20.7,169.7,61.4,162.7,3.0,14.1,45.6,59.5)))

  gam_trainW$OutMean <- as.integer(gam_trainW$OutMean)
  gam_trainW$rainl5 <- as.numeric(gam_trainW$rainl5)
  gam_trainW$rainl7 <- as.numeric(gam_trainW$rainl7)
  gam_trainW$rainl8 <- as.numeric(gam_trainW$rainl8)
  gam_trainW$rainl9 <- as.numeric(gam_trainW$rainl9)

  gamW <- gam(OutMean ~ s(rainl5,k=4) + s(rainl7,k=4) + s(rainl8,k=4) + s(rainl9,k=4),
              family=binomial, na.action=na.exclude, data=gam_trainW)

  gamW_test <- data.frame(rainl5,rainl7,rainl8,rainl9)

  gam.prediction.w <- round(predict(gamW, type="response", newdata=as.data.frame(gamW_test)),0)

}
alramadona/denfor documentation built on May 10, 2017, 11:11 p.m.