Problem Set # 5

Olivia Williams

date()
## [1] "Mon Nov 26 13:11:08 2012"

Due Date: November 29, 2012
Total Points: 50

The file Georgia.zip contains ESRI shape files in a folder called GeorgiaEduc. The dbf contains the percentage of Georgia county residents with a bachelors degree along with other countywide information. Let the dependent variable be PctBach (percent of population with a bachelor's degree) and the explanatory variables be TotPop90, PctRural, PctEld, PctFB, PctPov, PctBlack. Used the file lecture 16, used readShapeSpatial() lecture 20.

a. Download the zip file, unzip it and use the readShapeSpatial() function from the maptools package to get the data into R. Hint: After unzipping the shape files are in the directory Georgia. (10)

suppressMessages(require(maptools))
tmp = download.file("http://myweb.fsu.edu/jelsner/Georgia.zip", "Georgia.zip", 
    mode = "wb")
unzip("Georgia.zip")
GA = readShapeSpatial("Georgia/GeorgiaEduc")

b. Start with a multiple regression model using all six explanatory variables listed above. Create a final model by removing variables that are not significant in explaining percentage of bachelor degrees. (10)

model_full = lm(PctBach ~ TotPop90 + PctRural + PctEld + PctFB + PctPov + PctBlack, 
    data = GA)
step(model_full)
## Start:  AIC=427.2
## PctBach ~ TotPop90 + PctRural + PctEld + PctFB + PctPov + PctBlack
## 
##            Df Sum of Sq  RSS AIC
## - PctEld    1       5.3 1876 426
## - PctBlack  1       8.6 1879 426
## <none>                  1870 427
## - PctPov    1      61.7 1932 431
## - PctRural  1     120.5 1991 436
## - PctFB     1     195.9 2066 443
## - TotPop90  1     292.4 2163 450
## 
## Step:  AIC=425.7
## PctBach ~ TotPop90 + PctRural + PctFB + PctPov + PctBlack
## 
##            Df Sum of Sq  RSS AIC
## - PctBlack  1      10.9 1886 425
## <none>                  1876 426
## - PctPov    1     100.1 1976 433
## - PctRural  1     137.1 2013 436
## - PctFB     1     228.9 2104 444
## - TotPop90  1     287.2 2163 448
## 
## Step:  AIC=424.7
## PctBach ~ TotPop90 + PctRural + PctFB + PctPov
## 
##            Df Sum of Sq  RSS AIC
## <none>                  1886 425
## - PctPov    1       137 2024 435
## - PctRural  1       152 2038 436
## - PctFB     1       228 2114 443
## - TotPop90  1       320 2206 450
## 
## Call:
## lm(formula = PctBach ~ TotPop90 + PctRural + PctFB + PctPov, 
##     data = GA)
## 
## Coefficients:
## (Intercept)     TotPop90     PctRural        PctFB       PctPov  
##    1.44e+01     2.37e-05    -4.64e-02     1.30e+00    -1.31e-01
model = lm(PctBach ~ TotPop90 + PctRural + PctFB + PctPov, data = GA)

c. Use the significant explanatory variables and create a geographic regression model using a fixed bandwidth. Plot a choropleth map of the predictions from the model. (10)

install.packages("spgwr")
## Installing package(s) into
## 'C:/Users/owilliams/Documents/R/win-library/2.15' (as 'lib' is
## unspecified)
## Error: trying to use CRAN without setting a mirror
suppressMessages(require(spgwr))
## Warning: package 'spgwr' was built under R version 2.15.2
names(GA)
##  [1] "FID_1"     "AREA"      "PERIMETER" "G_UTM_"    "G_UTM_ID" 
##  [6] "AREANAME"  "AREAKEY"   "X_COORD"   "Y_COORD"   "KEY_VAL"  
## [11] "FID_2"     "AreaKey_1" "Latitude"  "Longitud"  "TotPop90" 
## [16] "PctRural"  "PctBach"   "PctEld"    "PctFB"     "PctPov"   
## [21] "PctBlack"  "ID"        "X"         "Y"         "Distance"
GA.bw = gwr.sel(PctBach ~ TotPop90 + PctRural + PctFB + PctPov, data = GA, coords = cbind(GA$X_COORD, 
    GA$Y_COORD))  #why does it say 'ignoring coords argument'?
## Warning: data is Spatial* object, ignoring coords argument
## Bandwidth: 241605 CV score: 2012 
## Bandwidth: 390534 CV score: 2052 
## Bandwidth: 149561 CV score: 1995 
## Bandwidth: 92675 CV score: 2100 
## Bandwidth: 184719 CV score: 1993 
## Bandwidth: 173020 CV score: 1991 
## Bandwidth: 170165 CV score: 1991 
## Bandwidth: 167827 CV score: 1991 
## Bandwidth: 168455 CV score: 1991 
## Bandwidth: 168480 CV score: 1991 
## Bandwidth: 168474 CV score: 1991 
## Bandwidth: 168474 CV score: 1991 
## Bandwidth: 168474 CV score: 1991 
## Bandwidth: 168476 CV score: 1991 
## Bandwidth: 168475 CV score: 1991 
## Bandwidth: 168474 CV score: 1991 
## Bandwidth: 168474 CV score: 1991 
## Bandwidth: 168474 CV score: 1991 
## Bandwidth: 168474 CV score: 1991 
## Bandwidth: 168474 CV score: 1991 
## Bandwidth: 168474 CV score: 1991
GA.gwr = gwr(PctBach ~ TotPop90 + PctRural + PctFB + PctPov, data = GA, coords = cbind(GA$X_COORD, 
    GA$Y_COORD), bandwidth = GA.bw)
## Warning: data is Spatial* object, ignoring coords argument
GA.gwr
## Call:
## gwr(formula = PctBach ~ TotPop90 + PctRural + PctFB + PctPov, 
##     data = GA, coords = cbind(GA$X_COORD, GA$Y_COORD), bandwidth = GA.bw)
## Kernel function: gwr.Gauss 
## Fixed bandwidth: 168474 
## Summary of GWR coefficient estimates:
##                   Min.   1st Qu.    Median   3rd Qu.      Max. Global
## X.Intercept.  1.12e+01  1.33e+01  1.45e+01  1.52e+01  1.58e+01  14.39
## TotPop90      1.35e-05  1.83e-05  2.20e-05  2.62e-05  3.37e-05   0.00
## PctRural     -6.24e-02 -5.14e-02 -4.46e-02 -3.89e-02 -2.58e-02  -0.05
## PctFB         4.54e-01  9.40e-01  1.43e+00  2.00e+00  2.52e+00   1.30
## PctPov       -1.46e-01 -1.39e-01 -1.34e-01 -1.21e-01 -9.26e-02  -0.13
df = slot(GA.gwr$SDF, "data")
names(df)
## [1] "sum.w"        "X.Intercept." "TotPop90"     "PctRural"    
## [5] "PctFB"        "PctPov"       "gwr.e"        "pred"        
## [9] "localR2"

brks = round(quantile(df$pred, probs = seq(0, 1, 0.2)), digits = 2)
ints = findInterval(df$pred, brks, all.inside = TRUE)
cls = rev(heat.colors(5))
par(mfrow = c(1, 1))
plot(GA, col = cls[ints])
legend(x = "topright", legend = leglabs(brks), cex = 0.9, fill = cls)
title(main = "Predicted (GWR) Percentage of Residents wtih Bachelor's Degrees")

plot of chunk unnamed-chunk-4

d. Plot a choropleth map of the percent poverty coefficient. (10)

brks = round(quantile(GA$PctPov, probs = seq(0, 1, 0.2)), digits = 2)
ints = findInterval(GA$PctPov, brks, all.inside = TRUE)
plot(GA, col = cls[ints])
legend(x = "topright", legend = leglabs(brks), cex = 0.9, fill = cls)
title(main = "Percent Poverty Coefficient")

plot of chunk unnamed-chunk-5

e. Plot a choropleth map of the R squared value. (10).

brks = cut(df$localR2, 6)
ints = as.integer(brks)
cls = rev(terrain.colors(6))
plot(GA, col = cls[ints])
legend(x = "topright", legend = levels(brks), fill = cls, bty = "n", title = "Local R Squared", 
    horiz = FALSE, cex = 0.9)

plot of chunk unnamed-chunk-6