lab3.R

arosati — Apr 1, 2013, 11:39 AM

library(spdep)
Loading required package: sp
Loading required package: boot
Loading required package: Matrix
Loading required package: lattice
Attaching package: 'lattice'
The following object(s) are masked from 'package:boot':

melanoma
Loading required package: MASS
Loading required package: nlme
Loading required package: maptools
Loading required package: foreign
Loading required package: grid
Checking rgeos availability: FALSE Note: when rgeos is not available,
polygon geometry computations in maptools depend on gpclib, which has a
restricted licence. It is disabled by default; to enable gpclib, type
gpclibPermit()
Loading required package: deldir
deldir 0.0-21
Loading required package: coda
Loading required package: splines
library(maptools)

library(rgdal)
rgdal: version: 0.8-5, (SVN revision 449) Geospatial Data Abstraction
Library extensions to R successfully loaded Loaded GDAL runtime: GDAL
1.9.0, released 2011/12/29 Path to GDAL shared files:
/Applications/RStudio.app/Contents/Resources/R/library/rgdal/gdal Loaded
PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480] Path to PROJ.4
shared files:
/Applications/RStudio.app/Contents/Resources/R/library/rgdal/proj
library(classInt)
Loading required package: class
Loading required package: e1071
library(gstat)

data <- readShapePoly(file.choose())
Error: file choice cancelled
summary(data)
Error: object of type 'closure' is not subsettable

data$RACE <- data$PROHISP + data$PROBLCK
Error: object of type 'closure' is not subsettable
names(data)
NULL

#model poverty according to structural variables - county-level employment, race, household/family composition, and county-level education
#PROPOV Proportion of the population <18 years living in poverty in 1989 
#RACE is PROBLCK Proportion non-Hispanic black and PROHISP Proportion Hispanic combined 
#PROAUNEM Proportion of all workers age 16+ who are unemployed
#PROHSLS Proportion with a high school education or less
#PROFEMHH Proportion female-headed families with children
pov_lm1.1 <- lm(data$PROPOV ~ data$PROHISP)
Error: object of type 'closure' is not subsettable
pov_lm1.2 <- lm(data$PROPOV ~ data$PROBLCK)
Error: object of type 'closure' is not subsettable

pov_lm1 <- lm(data$PROPOV ~ data$RACE)
Error: object of type 'closure' is not subsettable
pov_lm2 <- lm(data$PROPOV ~ data$PROAUNEM)
Error: object of type 'closure' is not subsettable
pov_lm3 <- lm(data$PROPOV ~ data$PROHSLS)
Error: object of type 'closure' is not subsettable
pov_lm4 <- lm(data$PROPOV ~ data$PROFEMHH)
Error: object of type 'closure' is not subsettable
anova(pov_lm1, pov_lm2, pov_lm3, pov_lm4) #not sig
Error: object 'pov_lm1' not found

pov_lm5 <- lm(data$PROPOV ~ data$RACE + data$PROAUNEM)
Error: object of type 'closure' is not subsettable
pov_lm6 <- lm(data$PROPOV ~ data$RACE + data$PROHSLS)
Error: object of type 'closure' is not subsettable
pov_lm7 <- lm(data$PROPOV ~ data$RACE + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
anova(pov_lm5, pov_lm6, pov_lm7) #not sig
Error: object 'pov_lm5' not found

pov_lm8 <- lm(data$PROPOV ~ data$PROAUNEM + data$PROHSLS)
Error: object of type 'closure' is not subsettable
pov_lm9 <- lm(data$PROPOV ~ data$PROAUNEM + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
pov_lm0 <- lm(data$PROPOV ~ data$PROHSLS + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
anova(pov_lm8, pov_lm9, pov_lm0, test="Chisq") #not sig
Error: object 'pov_lm8' not found

pov_lm11 <- lm(data$PROPOV ~ data$RACE + data$PROAUNEM + data$PROHSLS)
Error: object of type 'closure' is not subsettable
pov_lm12 <- lm(data$PROPOV ~ data$PROAUNEM + data$PROHSLS + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
pov_lm13 <- lm(data$PROPOV ~ data$RACE + data$PROHSLS + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
pov_lm14 <- lm(data$PROPOV ~ data$RACE + data$PROAUNEM + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
anova(pov_lm11, pov_lm12, pov_lm13, pov_lm14) #not sig
Error: object 'pov_lm11' not found

summary(pov_lm1.2)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm1.2' not found
summary(pov_lm2) #poverty modeled by employment
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm2' not found
summary(pov_lm3)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm3' not found
summary(pov_lm4)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm4' not found
summary(pov_lm5)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm5' not found
summary(pov_lm6)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm6' not found
summary(pov_lm7)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm7' not found
summary(pov_lm8)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm8' not found
summary(pov_lm9)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm9' not found
summary(pov_lm0)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm0' not found
summary(pov_lm11)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm11' not found
summary(pov_lm12)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm12' not found
summary(pov_lm13)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm13' not found
summary(pov_lm14)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm14' not found

#Interaction
pov_interact <-lm(data$PROPOV ~ data$PROAUNEM*(data$RACE+data$PROHSLS))
Error: object of type 'closure' is not subsettable
pov_interact2<- lm(data$PROPOV ~ (data$PROAUNEM*data$RACE) + (data$PROAUNEM*data$PROHSLS) + data$PROFEMHH + data$RACE+ data$PROHSLS +data$PROAUNEM)
Error: object of type 'closure' is not subsettable
summary(pov_interact)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_interact' not found
summary(pov_interact2)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_interact2' not found
inter<-stepAIC(pov_interact2, direction="both")
Error: object 'pov_interact2' not found
inter$anova
Error: object 'inter' not found
anova(pov_lm, pov_lm2, pov_interact2, pov_interact, test="Chisq")
Error: object 'pov_lm' not found
anova(pov_interact2, pov_interact)
Error: object 'pov_interact2' not found
plot(pov_interact)
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'pov_interact' not found
anova(pov_interact2, pov_lm, pov_lm2)
Error: object 'pov_interact2' not found

#library(leaps)
#auto<-regsubsets(PROPOV~.,nbest=1, data=data, really.big=T)

#Question 1 - How do different measures of county-level employment affect child poverty?
###############and Question 2
plot(pov_lm2)
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'pov_lm2' not found
shapiro.test(data$PROPOV) #data are not normally distributed
Error: object of type 'closure' is not subsettable
#shapiro.test(pov_lm2) #not all arguements have the same length
#qqnorm(pov_lm2)
plot(data$PROAUNEM, data$PROPOV, main="Scatterplot of Poverty and Unemployment",
     xlab="Proportion of Unemployment", ylab="Proportion of Poverty")
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error in data$PROAUNEM : object of type 'closure' is not
subsettable
abline(lm(data$PROPOV~data$PROAUNEM))
Error: object of type 'closure' is not subsettable
summary(pov_lm2)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm2' not found
coefficients(pov_lm2)
Error: object 'pov_lm2' not found
step<-stepAIC(pov_lm, direction="both")
Error: object 'pov_lm' not found
step$anova
Error: object of type 'closure' is not subsettable
step<-stepAIC(povertylm, direction="both")
Error: object 'povertylm' not found
step$anova
Error: object of type 'closure' is not subsettable
# run standard OLS regression - Question 2

povertylm <- lm(data$PROPOV ~ data$PROHISP + data$PROAUNEM + data$PROHSLS + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
povertylm2 <- lm(data$PROPOV ~ data$PROHISP + data$PROBLCK + data$PROAUNEM + data$PROHSLS + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
summary(povertylm2)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'povertylm2' not found

pov_lm <- lm(data$PROPOV ~ data$RACE + data$PROAUNEM + data$PROHSLS + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
pov_lm_blk <- lm(data$PROPOV ~ data$PROBLCK + data$PROAUNEM + data$PROHSLS + data$PROFEMHH)
Error: object of type 'closure' is not subsettable
summary(pov_lm)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm' not found
summary(pov_lm_blk) #black is significant, but MUCH less than hispanic!
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lm_blk' not found
summary(povertylm)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'povertylm' not found
anova(povertylm, pov_lm) #not significant
Error: object 'povertylm' not found
anova(povertylm, pov_lm_blk) #not significant
Error: object 'povertylm' not found

anova(pov_lm2, pov_lm) #sig differences as the model grows
Error: object 'pov_lm2' not found


###############Question 3 - Spatial Autocorrelation ######################

poverty_nbq <- poly2nb(data) # create a neighbors list 
Error: extends(class(pl), "SpatialPolygons") is not TRUE
poverty_nbq_wb <- nb2listw(poverty_nbq, style = "B", zero.policy = T)
Error: object 'poverty_nbq' not found
coords <- coordinates(data)
Error: unable to find an inherited method for function 'coordinates' for
signature '"function"'
FIPS <- row.names(as(data, "data.frame"))  # Define the fips code so that I can use K nearest neighbors
Error: no method or default for coercing "function" to "data.frame"
poverty_kn1 <- knn2nb(knearneigh(coords, k = 1), row.names = FIPS)
Error: object 'coords' not found
poverty_kn1_w <- nb2listw(poverty_kn1, zero.policy = T)
Error: object 'poverty_kn1' not found
poverty_kn2 <- knn2nb(knearneigh(coords, k = 2), row.names = FIPS)
Error: object 'coords' not found
poverty_kn2_w <- nb2listw(poverty_kn2, zero.policy = T)
Error: object 'poverty_kn2' not found

# run Moran's I of the residuals
data$lmresid <- residuals(povertylm)
Error: object 'povertylm' not found
data$lmresid2 <- residuals(pov_lm)
Error: object 'pov_lm' not found
lm.morantest(povertylm, poverty_kn1_w)
Error: object 'poverty_kn1_w' not found
data$lmresid3 <- residuals(pov_lm2)
Error: object 'pov_lm2' not found
lm.morantest(pov_lm2, poverty_kn1_w)
Error: object 'poverty_kn1_w' not found
lm.morantest(pov_lm, poverty_kn1_w)
Error: object 'poverty_kn1_w' not found


moran.test(data$PROPOV, listw = poverty_nbq_wb, zero.policy = T)
Error: object 'poverty_nbq_wb' not found


cor <- sp.correlogram(poverty_nbq, data$PROPOV, order = 5, method = "I", style = "W", zero.policy = T)
Error: object 'poverty_nbq' not found

cor_race <- sp.correlogram(poverty_nbq, data$PROPOV+data$RACE, order = 5, method = "I", style = "W", zero.policy = T)
Error: object 'poverty_nbq' not found
cor_unemp <- sp.correlogram(poverty_nbq, data$PROPOV+data$PROAUNEM, order = 5, method = "I", style = "W", zero.policy = T)
Error: object 'poverty_nbq' not found
cor_fem <- sp.correlogram(poverty_nbq, data$PROPOV+data$PROFEMHH, order = 5, method = "I", style = "W", zero.policy = T)
Error: object 'poverty_nbq' not found
cor_edu <- sp.correlogram(poverty_nbq, data$PROPOV+data$PROHSLS, order = 5, method = "I", style = "W", zero.policy = T)
Error: object 'poverty_nbq' not found
cor_all <- sp.correlogram(poverty_nbq, data$PROPOV+data$PROHSLS+data$RACE+data$PROAUNEM+data$PROFEMHH, order = 5, method = "I", style = "W", zero.policy = T)
Error: object 'poverty_nbq' not found


print(cor)
function (x, y = NULL, use = "everything", method = c("pearson", 
    "kendall", "spearman")) 
{
    na.method <- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs", 
        "everything", "na.or.complete"))
    if (is.na(na.method)) 
        stop("invalid 'use' argument")
    method <- match.arg(method)
    if (is.data.frame(y)) 
        y <- as.matrix(y)
    if (is.data.frame(x)) 
        x <- as.matrix(x)
    if (!is.matrix(x) && is.null(y)) 
        stop("supply both 'x' and 'y' or a matrix-like 'x'")
    if (!(is.numeric(x) || is.logical(x))) 
        stop("'x' must be numeric")
    stopifnot(is.atomic(x))
    if (!is.null(y)) {
        if (!(is.numeric(y) || is.logical(y))) 
            stop("'y' must be numeric")
        stopifnot(is.atomic(y))
    }
    Rank <- function(u) {
        if (length(u) == 0L) 
            u
        else if (is.matrix(u)) {
            if (nrow(u) > 1L) 
                apply(u, 2L, rank, na.last = "keep")
            else row(u)
        }
        else rank(u, na.last = "keep")
    }
    if (method == "pearson") 
        .Internal(cor(x, y, na.method, FALSE))
    else if (na.method %in% c(2L, 5L)) {
        if (is.null(y)) {
            .Internal(cor(Rank(na.omit(x)), NULL, na.method, 
                method == "kendall"))
        }
        else {
            nas <- attr(na.omit(cbind(x, y)), "na.action")
            dropNA <- function(x, nas) {
                if (length(nas)) {
                  if (is.matrix(x)) 
                    x[-nas, , drop = FALSE]
                  else x[-nas]
                }
                else x
            }
            .Internal(cor(Rank(dropNA(x, nas)), Rank(dropNA(y, 
                nas)), na.method, method == "kendall"))
        }
    }
    else if (na.method != 3L) {
        x <- Rank(x)
        if (!is.null(y)) 
            y <- Rank(y)
        .Internal(cor(x, y, na.method, method == "kendall"))
    }
    else {
        if (is.null(y)) {
            ncy <- ncx <- ncol(x)
            if (ncx == 0) 
                stop("'x' is empty")
            r <- matrix(0, nrow = ncx, ncol = ncy)
            for (i in seq_len(ncx)) {
                for (j in seq_len(i)) {
                  x2 <- x[, i]
                  y2 <- x[, j]
                  ok <- complete.cases(x2, y2)
                  x2 <- rank(x2[ok])
                  y2 <- rank(y2[ok])
                  r[i, j] <- if (any(ok)) 
                    .Internal(cor(x2, y2, 1L, method == "kendall"))
                  else NA
                }
            }
            r <- r + t(r) - diag(diag(r))
            rownames(r) <- colnames(x)
            colnames(r) <- colnames(x)
            r
        }
        else {
            if (length(x) == 0L || length(y) == 0L) 
                stop("both 'x' and 'y' must be non-empty")
            matrix_result <- is.matrix(x) || is.matrix(y)
            if (!is.matrix(x)) 
                x <- matrix(x, ncol = 1L)
            if (!is.matrix(y)) 
                y <- matrix(y, ncol = 1L)
            ncx <- ncol(x)
            ncy <- ncol(y)
            r <- matrix(0, nrow = ncx, ncol = ncy)
            for (i in seq_len(ncx)) {
                for (j in seq_len(ncy)) {
                  x2 <- x[, i]
                  y2 <- y[, j]
                  ok <- complete.cases(x2, y2)
                  x2 <- rank(x2[ok])
                  y2 <- rank(y2[ok])
                  r[i, j] <- if (any(ok)) 
                    .Internal(cor(x2, y2, 1L, method == "kendall"))
                  else NA
                }
            }
            rownames(r) <- colnames(x)
            colnames(r) <- colnames(y)
            if (matrix_result) 
                r
            else drop(r)
        }
    }
}
<bytecode: 0x10a5d0bc8>
<environment: namespace:stats>
print(cor_race)
Error: error in evaluating the argument 'x' in selecting a method for
function 'print': Error: object 'cor_race' not found
print(cor_unemp)
Error: error in evaluating the argument 'x' in selecting a method for
function 'print': Error: object 'cor_unemp' not found
print(cor_fem)
Error: error in evaluating the argument 'x' in selecting a method for
function 'print': Error: object 'cor_fem' not found
print(cor_edu)
Error: error in evaluating the argument 'x' in selecting a method for
function 'print': Error: object 'cor_edu' not found
print(cor_all)
Error: error in evaluating the argument 'x' in selecting a method for
function 'print': Error: object 'cor_all' not found


plot(cor, main="Correlogram of Poverty data only")
Error: supply both 'x' and 'y' or a matrix-like 'x'
plot(cor_race)
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'cor_race' not found
plot(cor_unemp)
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'cor_unemp' not found
plot(cor_fem)
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'cor_fem' not found
plot(cor_edu)
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'cor_edu' not found
plot(cor_all, main="Correlogram of Poverty Model")
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'cor_all' not found

plot(variogram(data$PROPOV ~ 1, locations = coordinates(data), data = data, cloud = F), 
     type = "b", pch = 16, main = "Variogram of PROPOV")
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error in function (classes, fdef, mtable) : unable to
find an inherited method for function 'coordinates' for signature
'"function"' Calls: variogram ... variogram.formula -> is -> coordinates
-> <Anonymous>
plot(variogram(data$PROPOV+data$PROAUNEM ~ 1, locations = coordinates(data), data = data, cloud = F), 
     type = "b", pch = 16, main = "Variogram of Poverty according to Unemployment")
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error in function (classes, fdef, mtable) : unable to
find an inherited method for function 'coordinates' for signature
'"function"' Calls: variogram ... variogram.formula -> is -> coordinates
-> <Anonymous>


# create a spatial lag model
#povertylag <- lagsarlm(data$PROPOV ~ data$PROHISP + data$PROHSLS + data$PROAUNEM + data$PROFEMHH, listw = poverty_kn1_w, zero.policy = T)
#summary(povertylag)
pov_lag <- lagsarlm(data$PROPOV ~ data$RACE + data$PROHSLS + data$PROAUNEM + data$PROFEMHH, listw = poverty_kn1_w, zero.policy = T)
Error: object of type 'closure' is not subsettable
summary(pov_lag)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_lag' not found
data$lagresid <- residuals(pov_lag)  
Error: object 'pov_lag' not found
moran.test(data$lagresid, listw = poverty_nbq_wb, zero.policy = T)
Error: object 'poverty_nbq_wb' not found


#data$lagresid <- residuals(povertylag)  # Test residuals for spatial autocorrelation
#moran.test(data$lagresid, listw = poverty_nbq_wb, zero.policy = T)

#data$lagresid_race <- residuals(pov_lag)  # Test residuals for spatial autocorrelation of hisp and black together
#moran.test(data$lagresid_race, listw = poverty_nbq_wb, zero.policy = T)


anova(pov_lag, pov_lm)
Error: object 'pov_lag' not found
#anova(pov_lag, povertylm)
#anova(povertylm, pov_lm)
#anova(pov_lag, pov_lm_blk)

#What would happen to poverty in the US if the unemployment rate rose dramatically in one place?
#head(data$Name)
#pov.new <- data
#pov.new@data[pov.new@data$Name=="Denver", "PUNEM"] <- .75
#I have no way of reading a county name or seeing the data directly in a way I understand. I can't run a test and I can't waste any more time since this is not a direct question of the lab
#el<-data.frame(name=data$Name, diff_in_pred_PROPOV=denver_effect)


#impacts(pov_lag, listw=poverty_nbq_wb) #says Rho is outside feasible range
impacts(pov_lag, listw=poverty_kn1_w) 
Error: object 'pov_lag' not found
impacts(pov_lag, listw=poverty_kn2_w) #kn2 changes the direct and indirect effects, but does NOT change the total AT ALL
Error: object 'pov_lag' not found

bptest.sarlm(pov_lag) #significant, so heteroskedastic, but can't go for perfection here...
Error: object 'pov_lag' not found

#######ERROR MODEL

#pov_err<-errorsarlm(data$PROPOV ~ data$PROHISP + data$PROHSLS + data$PROAUNEM + data$PROFEMHH, listw = poverty_kn1_w)
#summary(pov_err)
pov_err_race<-errorsarlm(data$PROPOV ~ data$RACE + data$PROHSLS + data$PROAUNEM + data$PROFEMHH, listw = poverty_kn1_w)
Error: object of type 'closure' is not subsettable
summary(pov_err_race)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'pov_err_race' not found

summary(lm.LMtests(pov_lm, listw=poverty_kn1_w, test="all"))
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error in lm.LMtests(pov_lm, listw = poverty_kn1_w,
test = "all") : object 'pov_lm' not found
moran.test(resid(pov_lag), poverty_kn1_w, randomisation=FALSE)
Error: object 'poverty_kn1_w' not found
#moran.test(resid(pov_err), poverty_kn1_w, randomisation=FALSE)
moran.test(resid(pov_err_race), poverty_kn1_w, randomisation=FALSE)
Error: object 'poverty_kn1_w' not found



####################try again with experiment test
pov.new<-data
str(pov.new@data)
Error: trying to get slot "data" from an object of a basic class
("function") with no slots
pov.new@data[grepl("Denver", pov.new@data$NAME), c("NAME", "PROAUNEM")]
Error: trying to get slot "data" from an object of a basic class
("function") with no slots

pov.new@data[pov.new@data$NAME == "Denver", "PROAUNEM"] <- 0.75
Error: trying to get slot "data" from an object of a basic class
("function") with no slots

pov.new@data[grepl("Denver", pov.new@data$NAME), c("NAME", "PROAUNEM")]
Error: trying to get slot "data" from an object of a basic class
("function") with no slots

# The original predicted values
orig.pred <- as.data.frame(predict(pov_lm))
Error: object 'pov_lm' not found
# The predicted values with the new unemployment rate in Denver
new.predK <- as.data.frame(predict(pov_lm, newdata = pov.new, listw = poverty_kn1_w, zero.policy = T))
Error: object 'pov_lm' not found
# The difference between the predicted values
denver_unempK <- new.predK$fit - orig.pred$fit
Error: object 'new.predK' not found
denver_unempK
Error: object 'denver_unempK' not found
pov.new$NAME
Error: object of type 'closure' is not subsettable
denver <- data.frame(name = data$NAME, diff_in_pred_PROPOV = denver_unempK)
Error: object of type 'closure' is not subsettable
pov.new$denver_unemp <- denver$diff_in_pred_PROPOV
Error: object 'denver' not found
# Sort counties by absolute value of the change in predicted PROPOV
abc <- abc[rev(order(abs(abc$diff_in_pred_PROPOV))), ]
Error: object 'abc' not found
abc[1:10, ]  # Show the top 10 counties
Error: object 'abc' not found


###################LISA map in R

##I used the save command to save an R-object of a spatial data frame. load("/Users/Seth/Desktop/soco.rda")
##Create row-standardized Queens contiguity weights matrix. soco_nbq<-poly2nb(soco) #Queen’s neighborhood
soco_nbq_w<- nb2listw(soco_nbq)
Error: object 'soco_nbq' not found
locm <- localmoran(data$PROPOV, poverty_kn1_w) #Calculate the local Morann Summary(locm)
Error: object of type 'closure' is not subsettable
##Manually make a moran plot
##Need to standardize variables
data$sPROPOV <- scale(data$PROPOV) #save to a new column
Error: object of type 'closure' is not subsettable
#create a lagged variable
data$lag_sPROPOV <- lag.listw(poverty_kn1_w, data$sPROPOV)
Error: object 'poverty_kn1_w' not found
summary(data$sPROPOV)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error in data$sPROPOV : object of type 'closure' is
not subsettable
summary(data$lag_sPROPOV)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error in data$lag_sPROPOV : object of type 'closure'
is not subsettable
plot(x= data$sPROPOV, y=data$lag_sPROPOV, main="Moran Scatterplot PROPOV") 
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error in data$sPROPOV : object of type 'closure' is not
subsettable
abline(h=0, v=0)
Error: plot.new has not been called yet
abline(lm(data$lag_sPROPOV ~ data$sPROPOV), lty=3, lwd=4, col="red")
Error: object of type 'closure' is not subsettable
##Check Out the outliers click on one or two and then hit escape (or click finish) identify(soco$sPPOV, soco$lag_sPPOV, soco$CNTY_ST, cex=0.8)
##Identify the Moran plot quadrant for each observation
#This is some serious slicing and illustrate the power of the bracket... 
data$quad_sig <- NA
Error: object of type 'closure' is not subsettable
data@data[(data$sPROPOV>= 0 & data$lag_sPROPOV>= 0) & (locm[,5]<= 0.05),"quad_sig"] <-1
Error: trying to get slot "data" from an object of a basic class
("function") with no slots
data@data[(data$sPROPOV<= 0 & data$lag_sPROPOV<= 0) & (locm[,5]<= 0.05),"quad_sig"] <-2
Error: trying to get slot "data" from an object of a basic class
("function") with no slots
data@data[(data$sPROPOV>= 0 & data$lag_sPROPOV<= 0) & (locm[,5]<= 0.05),"quad_sig"] <-3
Error: trying to get slot "data" from an object of a basic class
("function") with no slots
data@data[(data$sPROPOV>= 0 & data$lag_sPROPOV<= 0) & (locm[,5]<= 0.05),"quad_sig"] <-4
Error: trying to get slot "data" from an object of a basic class
("function") with no slots
data@data[(data$sPROPOV<= 0 & data$lag_sPROPOV>= 0) & (locm[,5]<= 0.05),"quad_sig"] <-5
Error: trying to get slot "data" from an object of a basic class
("function") with no slots
#####MAP THE RESULTS (courtesy of Paul Voss) ############# # Set the breaks for the thematic map classes
breaks <-seq(1,5,1)
# Set the corresponding labels for the thematic map classes
labels <- c("high-High", "low-Low", "High-Low", "Low-High", "Not Signif.")
#see ?findInterval - This is necessary for making a map 
np <- findInterval(data$quad_sig, breaks)
Error: object of type 'closure' is not subsettable
# Assign colors to each map class
colors <- c("red", "blue", "lightpink", "skyblue2", "white")
plot(data, col=colors[np]) #colors[np] manually sets the color for each county mtext("Local Moran's I", cex=1.5, side=3, line=1)
Error: object 'np' not found
legend("topleft", legend=labels, fill=colors, bty="n")
Error: plot.new has not been called yet

main="LISA map of poverty in 1989"