Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. library(sp)
  2. library(spatstat)
  3. library(dplyr)
  4. library(ggplot2)
  5. library(tidyverse)
  6. library(Rfast)
  7. library(fields)
  8. load("SHAMP_AEGISS_DATA.RData")
  9. objects()
  10.  
  11. casesidx <- over(SpatialPoints(cbind(x, y), proj4string = CRS(proj4string(shamp))),
  12. geometry(shamp))
  13. shamp$count <- sapply(1:146, function(x) {
  14. sum(casesidx == x)
  15. })
  16. crds <- coordinates(shamp)
  17. dat <- as.data.frame(shamp)
  18.  
  19.  
  20.  
  21. casesxy<-cbind(x,y)
  22. listofones<-rep(1,nrow(casesxy))
  23. casesxy<-cbind(casesxy,listofones)
  24. colnames(casesxy)<-c("x","y","label")
  25.  
  26. listofzeroes<-rep(0,nrow(controls))
  27. controlsxy<-cbind(controls,listofzeroes)
  28. colnames(controlsxy)<-c("x","y","label")
  29.  
  30. gasdata<-rbind(casesxy,controlsxy)
  31. gasdata<-as.data.frame(gasdata)
  32. gasdata$label<-as.factor(gasdata$label)
  33.  
  34. casesxy<-as.data.frame(casesxy)
  35. controlsxy<-as.data.frame(controlsxy)
  36. ### Exploratory Analysis ###
  37.  
  38. head(dat)
  39.  
  40. result1 <- aov(Health~Environment*Income,data = dat)
  41. print(summary(result1))
  42.  
  43. result2 <- aov(Health~Environment+Income,data = dat)
  44. print(summary(result2))
  45.  
  46. anova(result1,result2) # Health is affected by Environment and Income.
  47.  
  48. #
  49.  
  50. result3 <- aov(Income~Education*Employment,data = dat)
  51. print(summary(result3))
  52.  
  53. result4 <- aov(Income~Education+Employment,data = dat)
  54. print(summary(result4))
  55.  
  56. anova(result3,result4) # Income is earned similarly through education in either low or high employment.
  57.  
  58. #
  59.  
  60. result5 <- aov(Health~Barriers,data = dat)
  61. print(summary(result5)) # No significant relation between Health and Barriers.
  62.  
  63. result6 <- aov(Health~Crime,data = dat)
  64. print(summary(result6)) # Significant relation between Health and Crime.
  65.  
  66. plot(dat$Crime,dat$Health) # shows clear correlation. Health measures premature death, high health values means more death.
  67.  
  68. ## Conclude that Health is affected by Environment, Income, and Crime. We have Income affected by Employment and Education.
  69.  
  70. # Plot Distribution of Deprivation of Southhampton
  71. qplot(IMD, data = dat, geom = "density", alpha = I(0.5), main = "Distribution of Deprivation",
  72. xlab = "IMD Score", ylab = "Density")
  73.  
  74. ### South Hampton plot of cases and controls ###
  75.  
  76. simpwin <- simplify.owin(win, d = 200)
  77. gasppp <- ppp(x = gasdata$x, y = gasdata$y,marks = gasdata$label,
  78. window = simpwin)
  79. plot(gasppp, chars = c(1, 3), cols = c("red", "black"))
  80.  
  81. ### Testing for spatial clustering for cases ###
  82.  
  83. patt1 <- ppp(x = casesxy$x, y = casesxy$y,
  84. window = simpwin)
  85.  
  86. k1 <- Kest(patt1)
  87. plot(k1,pch = "+")
  88.  
  89. str(k1)
  90.  
  91. r1 <- k1$r
  92. kest1 <- k1$iso
  93.  
  94. plot(r1, kest1, type = "l")
  95. curve(pi * x^2, col = "red", lty = "dashed", add = TRUE)
  96.  
  97. # intensity
  98. lambdahat1 <- patt1$n/area.owin(patt1$window)
  99. lambdahat1 # 2.006403e-05
  100.  
  101.  
  102. nsims <- 20
  103. kfuns1 <- c() # initialise the object
  104. for (i in 1:nsims) {
  105. simdata <- rpoispp(lambda = lambdahat1, win = simpwin)
  106. ksim <- Kest(simdata, r = r1)$iso
  107. kfuns1 <- cbind(kfuns1, ksim)
  108. }
  109.  
  110. qts <- apply(kfuns1, 1, quantile, probs = c(0.025, 0.975))
  111.  
  112. plot(NULL, xlab = "r", ylab = "Estimated K function", xlim = range(r1),
  113. ylim = range(kfuns1), main="Cluster for cases")
  114. polygon(c(r1, rev(r1)), c(qts[1, ], rev(qts[2, ])), col = "lightgrey",
  115. border = NA)
  116. lines(r1, kest1)
  117. curve(pi * x^2, col = "red", lty = "dashed", add = TRUE)
  118.  
  119. ### Testing for spatial clustering for controls ###
  120.  
  121. patt2 <- ppp(x = controlsxy$x, y = controlsxy$y,
  122. window = simpwin)
  123.  
  124. k2 <- Kest(patt2)
  125. plot(k2,pch = "+")
  126.  
  127. str(k2)
  128.  
  129. r2 <- k2$r
  130. kest2 <- k2$iso
  131.  
  132. plot(r2, kest2, type = "l")
  133. curve(pi * x^2, col = "red", lty = "dashed", add = TRUE)
  134.  
  135. # intensity
  136. lambdahat2 <- patt2$n/area.owin(patt2$window)
  137. lambdahat2 # 2.010428e-05
  138.  
  139.  
  140. nsims <- 20
  141. kfuns2 <- c() # initialise the object
  142. for (i in 1:nsims) {
  143. simdata2 <- rpoispp(lambda = lambdahat2, win = simpwin)
  144. ksim2 <- Kest(simdata2, r = r1)$iso
  145. kfuns2 <- cbind(kfuns2, ksim2)
  146. }
  147.  
  148. qts2 <- apply(kfuns2, 1, quantile, probs = c(0.025, 0.975))
  149.  
  150. plot(NULL, xlab = "r", ylab = "Estimated K function", xlim = range(r2),
  151. ylim = range(kfuns2), main="Cluster for controls")
  152. polygon(c(r2, rev(r2)), c(qts2[1, ], rev(qts2[2, ])), col = "lightgrey",
  153. border = NA)
  154. lines(r2, kest2)
  155. curve(pi * x^2, col = "red", lty = "dashed", add = TRUE)
  156.  
  157. # Evidence of a cluster process since K-function lies above the tolerance envelope.
  158. # reduced number of sims to 20 due to limited processing power
  159.  
  160. cases <- patt1
  161. contr <- patt2
  162.  
  163. kcases <- Kest(cases)
  164. rk <- kcases$r
  165. kcontr <- Kest(contr, r = rk)
  166.  
  167. D <- kcases$iso - kcontr$iso
  168. plot(rk, D, xlab = "r", ylab = "D(r)", type = "l")
  169.  
  170. #
  171.  
  172. Dsim <- c() # we'll store our result in this object
  173. xk <- c(cases$x, contr$x)
  174. yk <- c(cases$y, contr$y)
  175. cc <- c(rep("case", cases$n), rep("control", contr$n))
  176. for (i in 1:nsims) {
  177. cc <- sample(cc)
  178. simcases <- ppp(x = xk[cc == "case"], y = yk[cc == "case"],
  179. window = simpwin)
  180. simcontr <- ppp(x = xk[cc == "control"], y = yk[cc == "control"],
  181. window = simpwin)
  182. dsim <- Kest(simcases, r = rk, correction = "isotropic")$iso -
  183. Kest(simcontr, r = rk, correction = "isotropic")$iso
  184. Dsim <- cbind(Dsim, dsim)
  185. }
  186.  
  187. qts3 <- apply(Dsim, 1, quantile, probs = c(0.025, 0.975))
  188.  
  189. # Plot
  190.  
  191. plot(NULL, xlab = "r", ylab = "Estimated K function", xlim = range(rk),
  192. ylim = range(Dsim))
  193. polygon(c(rk, rev(rk)), c(qts3[1, ], rev(qts3[2, ])), col = "lightgrey",
  194. border = NA)
  195. lines(rk, D)
  196. abline(h = 0, col = "red", lty = "dashed")
  197.  
  198. ### Estimating Intensity ###
  199.  
  200. den1 <- density(patt1, positive = TRUE)
  201. plot(den1,main="Density of cases")
  202.  
  203. xvals1 <- den1$xcol # the x-values
  204. yvals1 <- den1$yrow # the y-values
  205. zvals1 <- t(den1$v) # the estimated intensity, transposed
  206. image.plot(xvals1, yvals1, zvals1, asp = 1)
  207.  
  208. ### Generalised Linear Model ###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement