library(RODBC)
library(mongolite)
library(knitr)
library(psych)
library(kableExtra)
library(stringr)
library(dplyr)
library(tidyr)
library(scales)
library(ggplot2)
library(plotly)
library(maps)
library(mapdata)
library(ggrepel) #not using this at the moment, but it does give the option to add labels.  While not useful for the 

options(knitr.table.format = "html")
mrespir <- mongo("respir")
respirDF <- mrespir$find(
  query = '{"yearDiagnosis" : { "$lt" : 2001 }, "survivalMonths" : { "$lt" : 9999 } }', 
  fields = '{ "locality" : true, "ageDiagnosis" : true, "yearDiagnosis" : true, "survivalMonths" : true, "_id" : false }')
respirDF <- na.omit(respirDF)
nrow(respirDF)
## [1] 476824
respirDF <- mutate(respirDF,survivalYears = survivalMonths/12, currentYear = survivalYears + yearDiagnosis)
respirDF <- respirDF[ which(respirDF$currentYear < 2016), ]

ruralGeorgiaDF <- respirDF[ which(respirDF$locality == "Rural Georgia"), ]
iowaDF <- respirDF[ which(respirDF$locality == "Iowa"), ]
seattleDF <- respirDF[ which(respirDF$locality == "Seattle"), ]
atlantaDF <- respirDF[ which(respirDF$locality == "Atlanta"), ]
plot_ss <- function(x, y, maintitle, showSquares = FALSE, leastSquares = FALSE){
  plot(x,y,xlab="Diagnosis Year", ylab = "Survival Years", main = maintitle)

  if(leastSquares){
    m1 <- lm(y~x)
    y.hat <- m1$fit
  } else{
    pt1 <- locator(1)
    points(pt1$x, pt1$y, pch = 4)
    pt2 <- locator(1)
    points(pt2$x, pt2$y, pch = 4)
    pts <- data.frame("x" = c(pt1$x, pt2$x),"y" = c(pt1$y, pt2$y))
    m1 <- lm(y ~ x, data = pts)
    y.hat <- predict(m1, newdata = data.frame(x))
  }
  r <- y - y.hat
  abline(m1)

  oSide <- x - r
  LLim <- par()$usr[1]
  RLim <- par()$usr[2]
  oSide[oSide < LLim | oSide > RLim] <- c(x + r)[oSide < LLim | oSide > RLim] # move boxes to avoid margins

  n <- length(y.hat)
  for(i in 1:n){
    lines(rep(x[i], 2), c(y[i], y.hat[i]), lty = 2, col = "blue")
    if(showSquares){
    lines(rep(oSide[i], 2), c(y[i], y.hat[i]), lty = 3, col = "orange")
    lines(c(oSide[i], x[i]), rep(y.hat[i],2), lty = 3, col = "orange")
    lines(c(oSide[i], x[i]), rep(y[i],2), lty = 3, col = "orange")
    }
  }

}

summaryTable <- function(cancerType,maintitle = ""){
  survivalYears = cancerType$survivalYears
  yearDiagnosis = cancerType$yearDiagnosis
  meanTable <- tapply(survivalYears,yearDiagnosis,mean)
  show(nrow(cancerType))
  show(describeBy(survivalYears, group = yearDiagnosis, mat=TRUE))
  barplot(meanTable,beside=T,col=c("#ee7700","#3333ff")
    ,main=maintitle,xlab="Diagnosis Year",ylab="Survival Years")
}


inferenceTests <- function(cancerType, maintitle = "") {
  yearDiagnosis <- cancerType$yearDiagnosis
  survivalYears <- cancerType$survivalYears
  plot_ss(x = yearDiagnosis, y = survivalYears, maintitle, showSquares = FALSE)
  m2 <- lm(survivalYears ~ yearDiagnosis, data = cancerType)
  summary(m2)
}

inferenceTest0 <- function(cancerType) {
  m2 <- lm(survivalYears ~ yearDiagnosis, data = cancerType)
  hist(m2$residuals)
  qqnorm(m2$residuals)
  qqline(m2$residuals)  
}
## [1] 476824
##      item group1 vars     n     mean       sd    median  trimmed     mad
## X11     1   1973    1  7421 2.759511 6.185634 0.5000000 1.118564 0.61775
## X12     2   1974    1  8975 3.156657 6.773337 0.5833333 1.331210 0.74130
## X13     3   1975    1  9930 2.980799 6.436963 0.5833333 1.250525 0.74130
## X14     4   1976    1 10578 3.065994 6.359016 0.6666667 1.373582 0.86485
## X15     5   1977    1 10951 3.083934 6.459985 0.6666667 1.353784 0.86485
## X16     6   1978    1 11476 3.093064 6.329695 0.6666667 1.405140 0.86485
## X17     7   1979    1 11948 2.942794 5.993471 0.6666667 1.350270 0.86485
## X18     8   1980    1 12449 2.875840 5.877824 0.6666667 1.330087 0.86485
## X19     9   1981    1 13013 2.939817 5.938792 0.6666667 1.364406 0.86485
## X110   10   1982    1 13401 2.980375 5.980149 0.6666667 1.390822 0.86485
## X111   11   1983    1 13598 3.004633 5.954391 0.6666667 1.423506 0.86485
## X112   12   1984    1 14167 2.826016 5.674059 0.6666667 1.317152 0.86485
## X113   13   1985    1 14237 2.929661 5.760715 0.6666667 1.397426 0.86485
## X114   14   1986    1 14625 2.861054 5.601615 0.7500000 1.373202 0.98840
## X115   15   1987    1 15200 2.778586 5.412748 0.6666667 1.339899 0.86485
## X116   16   1988    1 15459 2.777373 5.416707 0.6666667 1.338029 0.86485
## X117   17   1989    1 15489 2.783895 5.367042 0.7500000 1.348638 0.98840
## X118   18   1990    1 15855 2.811116 5.296980 0.7500000 1.401564 0.98840
## X119   19   1991    1 16239 2.788216 5.169718 0.7500000 1.414832 0.98840
## X120   20   1992    1 22357 2.728679 5.064413 0.7500000 1.381213 0.98840
## X121   21   1993    1 22112 2.681123 4.958204 0.6666667 1.359365 0.86485
## X122   22   1994    1 22107 2.724476 4.955791 0.7500000 1.395413 0.98840
## X123   23   1995    1 22427 2.694026 4.801400 0.7500000 1.416374 0.98840
## X124   24   1996    1 22490 2.688699 4.737620 0.6666667 1.428760 0.86485
## X125   25   1997    1 22569 2.600802 4.521878 0.7500000 1.402060 0.98840
## X126   26   1998    1 22943 2.590594 4.423947 0.7500000 1.417493 0.98840
## X127   27   1999    1 22905 2.577956 4.299276 0.7500000 1.451564 0.98840
## X128   28   2000    1 51903 2.482151 4.100670 0.7500000 1.413229 0.98840
##      min      max    range     skew  kurtosis         se
## X11    0 42.91667 42.91667 3.648284 14.984012 0.07180472
## X12    0 41.91667 41.91667 3.282687 11.524087 0.07149662
## X13    0 40.91667 40.91667 3.374960 12.203756 0.06459611
## X14    0 39.91667 39.91667 3.248832 11.373464 0.06182842
## X15    0 38.91667 38.91667 3.245890 11.136045 0.06173119
## X16    0 37.91667 37.91667 3.195686 10.780425 0.05908640
## X17    0 36.91667 36.91667 3.206208 11.026504 0.05483158
## X18    0 35.91667 35.91667 3.294079 11.680610 0.05268043
## X19    0 34.91667 34.91667 3.190019 10.770003 0.05206062
## X110   0 33.91667 33.91667 3.126128 10.170239 0.05165869
## X111   0 32.91667 32.91667 3.060926  9.619290 0.05106224
## X112   0 31.91667 31.91667 3.156159 10.310437 0.04767107
## X113   0 30.91667 30.91667 3.018698  9.211257 0.04827999
## X114   0 29.91667 29.91667 3.030200  9.280250 0.04631965
## X115   0 28.91667 28.91667 3.011478  9.172017 0.04390318
## X116   0 27.91667 27.91667 3.004268  8.998679 0.04356569
## X117   0 26.91667 26.91667 2.919218  8.338630 0.04312442
## X118   0 25.91667 25.91667 2.814148  7.628188 0.04206735
## X119   0 24.91667 24.91667 2.761625  7.297869 0.04056834
## X120   0 23.91667 23.91667 2.741123  7.082723 0.03387056
## X121   0 22.91667 22.91667 2.699955  6.786354 0.03334343
## X122   0 21.91667 21.91667 2.573823  5.927358 0.03333098
## X123   0 20.91667 20.91667 2.505410  5.544809 0.03206138
## X124   0 19.91667 19.91667 2.422418  5.019460 0.03159115
## X125   0 18.91667 18.91667 2.396506  4.878989 0.03009974
## X126   0 17.91667 17.91667 2.322820  4.433139 0.02920686
## X127   0 16.91667 16.91667 2.219334  3.884487 0.02840731
## X128   0 15.91667 15.91667 2.168232  3.615626 0.01799942

## [1] 825
##     item group1 vars   n     mean       sd    median  trimmed      mad min
## X11    1   1992    1  77 2.860390 5.741722 0.5833333 1.324074 0.864850   0
## X12    2   1993    1  76 2.908991 5.930355 0.4166667 1.303763 0.617750   0
## X13    3   1994    1  88 2.747159 4.594378 0.7916667 1.685185 1.050175   0
## X14    4   1995    1 102 2.653595 5.046862 0.5833333 1.270325 0.741300   0
## X15    5   1996    1  96 3.578125 6.033930 0.6250000 2.197650 0.803075   0
## X16    6   1997    1  90 2.545370 4.743238 0.7083333 1.239583 0.803075   0
## X17    7   1998    1  94 2.437057 4.378447 0.6250000 1.274123 0.926625   0
## X18    8   1999    1  96 2.181424 3.777763 0.6666667 1.212607 0.741300   0
## X19    9   2000    1 106 2.045597 3.455578 0.6666667 1.199612 0.864850   0
##          max    range     skew kurtosis        se
## X11 23.66667 23.66667 2.572490 5.509334 0.6543299
## X12 22.75000 22.75000 2.448242 4.813026 0.6802583
## X13 19.00000 19.00000 2.084868 3.298622 0.4897623
## X14 20.58333 20.58333 2.642679 5.911779 0.4997138
## X15 19.83333 19.83333 1.796668 1.714042 0.6158354
## X16 18.83333 18.83333 2.456884 4.966717 0.4999812
## X17 17.50000 17.50000 2.391588 4.655925 0.4516024
## X18 16.58333 16.58333 2.549530 5.884820 0.3855663
## X19 15.83333 15.83333 2.540504 6.026717 0.3356355

## [1] 55727
##      item group1 vars    n     mean       sd    median   trimmed     mad
## X11     1   1973    1 1392 2.286818 5.543744 0.4166667 0.8168761 0.61775
## X12     2   1974    1 1365 2.808303 6.239194 0.5000000 1.1534004 0.61775
## X13     3   1975    1 1476 2.559395 5.787729 0.5000000 1.0103638 0.61775
## X14     4   1976    1 1510 2.492108 5.683725 0.5000000 1.0029663 0.61775
## X15     5   1977    1 1533 2.983692 6.335230 0.5833333 1.2955039 0.74130
## X16     6   1978    1 1660 2.715311 5.759232 0.6666667 1.2113454 0.86485
## X17     7   1979    1 1741 2.669443 5.590374 0.5833333 1.1852716 0.74130
## X18     8   1980    1 1723 2.802815 5.906075 0.6666667 1.2606357 0.86485
## X19     9   1981    1 1783 2.738269 5.770348 0.6666667 1.1940551 0.86485
## X110   10   1982    1 1802 2.685350 5.584857 0.6666667 1.2226653 0.86485
## X111   11   1983    1 1911 2.739142 5.524665 0.6666667 1.2868978 0.86485
## X112   12   1984    1 1969 2.525986 5.163679 0.6666667 1.1631262 0.86485
## X113   13   1985    1 2023 2.773892 5.633645 0.6666667 1.2774861 0.86485
## X114   14   1986    1 2032 2.455750 5.003860 0.6666667 1.1309451 0.86485
## X115   15   1987    1 2133 2.490702 5.009200 0.6666667 1.1766745 0.86485
## X116   16   1988    1 2209 2.478120 4.991872 0.6666667 1.1798097 0.86485
## X117   17   1989    1 2146 2.421482 4.802754 0.6666667 1.1630772 0.86485
## X118   18   1990    1 2162 2.758210 5.255628 0.7500000 1.3697495 0.98840
## X119   19   1991    1 2204 2.297716 4.429120 0.6666667 1.1571712 0.86485
## X120   20   1992    1 2313 2.465377 4.628796 0.7500000 1.2478840 0.98840
## X121   21   1993    1 2310 2.683622 4.942560 0.7500000 1.3681006 0.98840
## X122   22   1994    1 2283 2.583114 4.755223 0.7500000 1.3052363 0.98840
## X123   23   1995    1 2262 2.534520 4.568452 0.7500000 1.3220534 0.98840
## X124   24   1996    1 2412 2.581260 4.635202 0.6666667 1.3388601 0.86485
## X125   25   1997    1 2340 2.395798 4.265148 0.6666667 1.2581909 0.86485
## X126   26   1998    1 2330 2.498212 4.274278 0.7500000 1.3724964 0.98840
## X127   27   1999    1 2329 2.380313 4.112281 0.6666667 1.2820822 0.86485
## X128   28   2000    1 2374 2.333719 3.882913 0.7500000 1.3216228 0.98840
##      min      max    range     skew  kurtosis         se
## X11    0 42.75000 42.75000 4.112676 20.115519 0.14858795
## X12    0 41.33333 41.33333 3.449792 12.866491 0.16887377
## X13    0 40.91667 40.91667 3.577836 13.905163 0.15064857
## X14    0 39.91667 39.91667 3.840127 16.728247 0.14626639
## X15    0 38.83333 38.83333 3.272845 11.377560 0.16180477
## X16    0 37.91667 37.91667 3.546520 13.811286 0.14135480
## X17    0 36.75000 36.75000 3.413722 12.874838 0.13398047
## X18    0 35.91667 35.91667 3.460871 12.774332 0.14228409
## X19    0 34.75000 34.75000 3.343791 11.912719 0.13665526
## X110   0 33.75000 33.75000 3.403441 12.555284 0.13156328
## X111   0 32.83333 32.83333 3.234937 11.285380 0.12637919
## X112   0 31.91667 31.91667 3.363086 12.202542 0.11636875
## X113   0 30.91667 30.91667 3.122745  9.979852 0.12525397
## X114   0 29.91667 29.91667 3.343728 11.785782 0.11100520
## X115   0 28.91667 28.91667 3.266179 11.315053 0.10846083
## X116   0 27.91667 27.91667 3.291559 11.381897 0.10621004
## X117   0 26.91667 26.91667 3.263024 11.109555 0.10367535
## X118   0 25.91667 25.91667 2.948166  8.524504 0.11303079
## X119   0 24.91667 24.91667 3.206116 10.749598 0.09434343
## X120   0 23.91667 23.91667 2.968852  8.720803 0.09624545
## X121   0 22.91667 22.91667 2.684743  6.711237 0.10283618
## X122   0 21.91667 21.91667 2.749471  7.004133 0.09952173
## X123   0 20.91667 20.91667 2.620706  6.310807 0.09605561
## X124   0 19.91667 19.91667 2.494368  5.428908 0.09438001
## X125   0 18.91667 18.91667 2.561480  5.886903 0.08817109
## X126   0 17.91667 17.91667 2.418488  5.051635 0.08854924
## X127   0 16.91667 16.91667 2.417558  4.907213 0.08521146
## X128   0 15.91667 15.91667 2.311109  4.447315 0.07969248

## [1] 55938
##      item group1 vars    n     mean       sd    median  trimmed     mad
## X11     1   1974    1 1180 3.204237 6.992102 0.5833333 1.313471 0.74130
## X12     2   1975    1 1221 2.782760 6.131645 0.5833333 1.172467 0.74130
## X13     3   1976    1 1373 3.012139 6.193540 0.6666667 1.386639 0.86485
## X14     4   1977    1 1423 3.079937 6.385239 0.5833333 1.385718 0.74130
## X15     5   1978    1 1492 3.103552 6.312145 0.6666667 1.404523 0.86485
## X16     6   1979    1 1539 2.996372 5.922174 0.6666667 1.411530 0.86485
## X17     7   1980    1 1603 2.539093 5.211366 0.5833333 1.191348 0.74130
## X18     8   1981    1 1764 2.894841 5.718171 0.6666667 1.391171 0.86485
## X19     9   1982    1 1793 2.881855 5.868799 0.6666667 1.321835 0.86485
## X110   10   1983    1 1848 3.169508 6.140032 0.7500000 1.549606 0.98840
## X111   11   1984    1 1982 2.795535 5.606517 0.6666667 1.324243 0.86485
## X112   12   1985    1 1980 3.091835 6.028601 0.7500000 1.485743 0.98840
## X113   13   1986    1 2056 2.883350 5.565604 0.7500000 1.427653 0.98840
## X114   14   1987    1 2133 2.778911 5.318100 0.7500000 1.388938 0.98840
## X115   15   1988    1 2133 2.875449 5.487126 0.7500000 1.439758 0.98840
## X116   16   1989    1 2171 2.932174 5.695959 0.7500000 1.387594 0.98840
## X117   17   1990    1 2425 2.702302 5.123175 0.7500000 1.347587 0.98840
## X118   18   1991    1 2387 2.912058 5.309726 0.7500000 1.509942 0.98840
## X119   19   1992    1 2491 2.658069 4.983422 0.7500000 1.323131 0.98840
## X120   20   1993    1 2432 2.694696 4.949916 0.6666667 1.375814 0.86485
## X121   21   1994    1 2486 2.701026 4.890813 0.6666667 1.395645 0.86485
## X122   22   1995    1 2533 2.789018 4.896627 0.7500000 1.489558 0.98840
## X123   23   1996    1 2528 2.662744 4.658131 0.7500000 1.438859 0.98840
## X124   24   1997    1 2569 2.586966 4.448022 0.7500000 1.415492 0.98840
## X125   25   1998    1 2784 2.600216 4.486626 0.7500000 1.399424 0.98840
## X126   26   1999    1 2868 2.597193 4.285758 0.7500000 1.483595 0.98840
## X127   27   2000    1 2744 2.508989 4.142617 0.7500000 1.426192 0.98840
##      min      max    range     skew  kurtosis         se
## X11    0 41.75000 41.75000 3.293916 11.400292 0.20354796
## X12    0 40.66667 40.66667 3.643311 14.699134 0.17547658
## X13    0 39.83333 39.83333 3.323780 12.060605 0.16714897
## X14    0 38.83333 38.83333 3.210555 10.887276 0.16926793
## X15    0 37.91667 37.91667 3.126139 10.235186 0.16341525
## X16    0 36.50000 36.50000 3.040016  9.768922 0.15095999
## X17    0 35.66667 35.66667 3.501245 13.710779 0.13016219
## X18    0 34.83333 34.83333 3.149136 10.561180 0.13614693
## X19    0 33.91667 33.91667 3.224810 10.921636 0.13859868
## X110   0 32.91667 32.91667 2.896933  8.490530 0.14283008
## X111   0 31.91667 31.91667 3.249514 11.148748 0.12593352
## X112   0 30.91667 30.91667 2.953447  8.634523 0.13548273
## X113   0 29.91667 29.91667 3.006894  9.333304 0.12274413
## X114   0 28.91667 28.91667 3.046893  9.636608 0.11514925
## X115   0 27.91667 27.91667 3.036567  9.270049 0.11880904
## X116   0 26.91667 26.91667 2.853706  7.696803 0.12224665
## X117   0 25.91667 25.91667 2.889182  8.215314 0.10403592
## X118   0 24.91667 24.91667 2.682966  6.791027 0.10867906
## X119   0 23.91667 23.91667 2.806363  7.455599 0.09984833
## X120   0 22.91667 22.91667 2.656187  6.548201 0.10037281
## X121   0 21.91667 21.91667 2.572378  5.987675 0.09809130
## X122   0 20.91667 20.91667 2.423424  5.062766 0.09729251
## X123   0 19.91667 19.91667 2.449478  5.244412 0.09264525
## X124   0 18.91667 18.91667 2.454536  5.224848 0.08775764
## X125   0 17.91667 17.91667 2.340639  4.459422 0.08503255
## X126   0 16.91667 16.91667 2.202121  3.825145 0.08002727
## X127   0 15.91667 15.91667 2.169893  3.605843 0.07908293

## [1] 28421
##      item group1 vars    n     mean       sd    median  trimmed      mad
## X11     1   1975    1  591 2.896785 6.474009 0.5000000 1.132311 0.741300
## X12     2   1976    1  715 3.556061 7.053970 0.6666667 1.654741 0.864850
## X13     3   1977    1  718 3.043640 6.685319 0.6666667 1.245660 0.864850
## X14     4   1978    1  778 3.149850 6.203901 0.6666667 1.491453 0.864850
## X15     5   1979    1  853 2.559398 5.191641 0.6666667 1.244510 0.864850
## X16     6   1980    1  811 2.841965 5.815425 0.6666667 1.329353 0.864850
## X17     7   1981    1  890 2.845599 5.950466 0.5833333 1.268727 0.741300
## X18     8   1982    1  969 3.270812 6.180148 0.7500000 1.643608 0.988400
## X19     9   1983    1  951 3.212233 6.076503 0.7500000 1.601949 0.988400
## X110   10   1984    1  954 2.930556 5.622383 0.8333333 1.447753 0.988400
## X111   11   1985    1 1060 3.140016 5.946109 0.7083333 1.581859 0.926625
## X112   12   1986    1 1063 3.131703 5.929834 0.7500000 1.563455 0.988400
## X113   13   1987    1 1126 3.099689 6.023074 0.7500000 1.469697 0.988400
## X114   14   1988    1 1187 2.823013 5.443260 0.7500000 1.370224 0.988400
## X115   15   1989    1 1216 2.672697 5.049252 0.7500000 1.349760 0.864850
## X116   16   1990    1 1191 3.053247 5.557475 0.8333333 1.584470 0.988400
## X117   17   1991    1 1238 2.864095 5.235347 0.7916667 1.502352 0.926625
## X118   18   1992    1 1326 3.220840 5.788431 0.8333333 1.665176 1.111950
## X119   19   1993    1 1290 2.963114 5.214844 0.8333333 1.597868 1.111950
## X120   20   1994    1 1281 2.798855 4.999881 0.6666667 1.474634 0.864850
## X121   21   1995    1 1296 3.131109 5.199620 0.8333333 1.795681 1.111950
## X122   22   1996    1 1329 2.960810 4.987424 0.8333333 1.666197 1.111950
## X123   23   1997    1 1438 2.823076 4.798965 0.7500000 1.564742 0.988400
## X124   24   1998    1 1373 2.518148 4.334654 0.6666667 1.359569 0.864850
## X125   25   1999    1 1348 2.639590 4.401125 0.7500000 1.482099 0.988400
## X126   26   2000    1 1429 2.456846 4.152160 0.6666667 1.356769 0.864850
##      min      max    range     skew  kurtosis        se
## X11    0 40.83333 40.83333 3.429340 12.482460 0.2663052
## X12    0 39.50000 39.50000 2.707379  6.993991 0.2638035
## X13    0 38.83333 38.83333 3.388747 11.880895 0.2494939
## X14    0 37.66667 37.66667 2.944825  8.991922 0.2224206
## X15    0 36.91667 36.91667 3.594817 14.814405 0.1777584
## X16    0 35.66667 35.66667 3.377506 12.746123 0.2042072
## X17    0 34.91667 34.91667 3.359004 12.044084 0.1994601
## X18    0 33.66667 33.66667 2.809188  7.911183 0.1985349
## X19    0 32.83333 32.83333 2.759302  7.448709 0.1970441
## X110   0 31.83333 31.83333 2.996582  9.201008 0.1820314
## X111   0 30.91667 30.91667 2.835942  8.090688 0.1826333
## X112   0 29.91667 29.91667 2.800456  7.708188 0.1818762
## X113   0 28.91667 28.91667 2.807641  7.412281 0.1794936
## X114   0 27.83333 27.83333 2.885763  8.137303 0.1579915
## X115   0 26.91667 26.91667 2.985370  9.022478 0.1447972
## X116   0 25.91667 25.91667 2.629674  6.427563 0.1610355
## X117   0 24.91667 24.91667 2.784513  7.465796 0.1487939
## X118   0 23.91667 23.91667 2.399690  4.813935 0.1589604
## X119   0 22.91667 22.91667 2.491010  5.557191 0.1451933
## X120   0 21.91667 21.91667 2.512379  5.633821 0.1396964
## X121   0 20.91667 20.91667 2.213912  3.959003 0.1444339
## X122   0 19.91667 19.91667 2.249542  4.089011 0.1368087
## X123   0 18.91667 18.91667 2.215008  3.836737 0.1265517
## X124   0 17.91667 17.91667 2.429745  5.002374 0.1169820
## X125   0 16.91667 16.91667 2.192800  3.677495 0.1198723
## X126   0 15.91667 15.91667 2.193686  3.654697 0.1098394

inferenceTests(respirDF, "Respiratory Cancer - All Localities - Inference Analysis")

## 
## Call:
## lm(formula = survivalYears ~ yearDiagnosis, data = cancerType)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.106 -2.552 -2.054 -0.589 39.811 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   43.4618520  1.9009166   22.86   <2e-16 ***
## yearDiagnosis -0.0204541  0.0009554  -21.41   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.268 on 476822 degrees of freedom
## Multiple R-squared:  0.0009603,  Adjusted R-squared:  0.0009582 
## F-statistic: 458.3 on 1 and 476822 DF,  p-value: < 2.2e-16
inferenceTests(ruralGeorgiaDF, "Respiratory Cancer - Rural Georgia - Inference Analysis")

## 
## Call:
## lm(formula = survivalYears ~ yearDiagnosis, data = cancerType)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.1013 -2.4427 -1.9005 -0.5923 20.5654 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept)   219.0896   132.7405   1.651   0.0992 .
## yearDiagnosis  -0.1084     0.0665  -1.631   0.1034  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.868 on 823 degrees of freedom
## Multiple R-squared:  0.00322,    Adjusted R-squared:  0.002009 
## F-statistic: 2.659 on 1 and 823 DF,  p-value: 0.1034
inferenceTests(iowaDF, "Respiratory Cancer - Iowa - Inference Analysis")

## 
## Call:
## lm(formula = survivalYears ~ yearDiagnosis, data = cancerType)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -2.714 -2.379 -1.906 -0.629 40.036 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   22.590842   5.468362   4.131 3.61e-05 ***
## yearDiagnosis -0.010075   0.002751  -3.662  0.00025 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.092 on 55725 degrees of freedom
## Multiple R-squared:  0.0002406,  Adjusted R-squared:  0.0002227 
## F-statistic: 13.41 on 1 and 55725 DF,  p-value: 0.0002503
inferenceTests(seattleDF, "Respiratory Cancer - Seattle - Inference Analysis")

## 
## Call:
## lm(formula = survivalYears ~ yearDiagnosis, data = cancerType)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.075 -2.574 -2.074 -0.619 38.675 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   39.894478   5.977923   6.674 2.52e-11 ***
## yearDiagnosis -0.018652   0.003006  -6.205 5.49e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.327 on 55936 degrees of freedom
## Multiple R-squared:  0.000688,   Adjusted R-squared:  0.0006701 
## F-statistic: 38.51 on 1 and 55936 DF,  p-value: 5.491e-10
inferenceTests(atlantaDF, "Respiratory Cancer - Atlanta - Inference Analysis")

## 
## Call:
## lm(formula = survivalYears ~ yearDiagnosis, data = cancerType)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.165 -2.694 -2.178 -0.590 37.669 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   36.744933   8.958108   4.102 4.11e-05 ***
## yearDiagnosis -0.017003   0.004504  -3.775  0.00016 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.466 on 28419 degrees of freedom
## Multiple R-squared:  0.0005013,  Adjusted R-squared:  0.0004661 
## F-statistic: 14.25 on 1 and 28419 DF,  p-value: 0.0001601