stats_test2_final.R

Toni — Apr 25, 2013, 7:37 PM

library(knitr)
#ny <- (file.choose())
library(spdep)
Loading required package: sp
Warning: package 'sp' was built under R version 2.15.3
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(sp)

summary(ny)
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'ny' not found
plot(ny)
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'ny' not found
leuk <- lm(ny$CASES~ny$PEXPOSURE+ny$PCTAGE65P+ny$PCTOWNHOME)
Error: object 'ny' not found
plot(leuk)

plot of chunk unnamed-chunk-1

#lm.LMtests(leuk) this needs weights. doesn't work here.
leuk2 <- lm(ny$CASES~ny$PCTAGE65P+ny$PCTOWNHOME)
Error: object 'ny' not found
summary(leuk) #pexosure is not significant!
      wbc               ag          time      
 Min.   :   750   absent :16   Min.   :  1.0  
 1st Qu.:  5300   present:17   1st Qu.:  4.0  
 Median : 10500                Median : 22.0  
 Mean   : 29165                Mean   : 40.9  
 3rd Qu.: 32000                3rd Qu.: 65.0  
 Max.   :100000                Max.   :156.0  
summary(leuk2) 
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'leuk2' not found
anova(leuk, leuk2) #the difference is not significant
Error: no applicable method for 'anova' applied to an object of class
"data.frame"

expos<-lm(ny$CASES~ny$PEXPOSURE)
Error: object 'ny' not found
summary(expos) #this is significant
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error: object 'expos' not found
anova(leuk, expos)
Error: no applicable method for 'anova' applied to an object of class
"data.frame"

plot(ny$CASES, ny$PEXPOSURE, main="Scatterplot of Leukemia Cases and Exposure to TCE",
     xlab="Proximity to Waste Site", ylab="Number of Cases")
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'ny' not found
abline(lm(ny$CASES~ny$PEXPOSURE))
Error: object 'ny' not found
plot(expos)
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error: object 'expos' not found
shapiro.test(ny$CASES)
Error: object 'ny' not found

ny$lmresid<-residuals(leuk)
Error: object 'ny' not found

shapiro.test(ny$CASES) #significant p=1.039e-10
Error: object 'ny' not found
coefficients(leuk)
NULL

step<-stepAIC(leuk, direction="both")
Error: no terms component nor attribute


#6.2.1 Running a Regression
#• using lm with a “formula,” y ∼ x1 + x2
#• assigning the regression results to an object
#• extracting the residuals as resid(regressionObject)
#6.2.2 Testing OLS Residuals for Spatial Autocorrelation
#• Moran test for residuals using lm.morantest
#• parameters: a regression object and a listw object
#• Lagrange Multiplier tests using lm.LMtests
#• parameters: a regression object, listw object and the type of test









#step 1
ny_nbq<-poly2nb(ny)
Error: object 'ny' not found
#step 2 assign weights
ny_nbqw<-nb2listw(ny_nbq) #row standardized weights matrix
Error: object 'ny_nbq' not found
ny_nbqwb<-nb2listw(ny_nbq, style="B") #binary weights
Error: object 'ny_nbq' not found
ny_nbqw
Error: object 'ny_nbqw' not found
#step 3 examine autocorrelation
#moran's I
leuk #the regression coefficient IS the Moran's I 
      wbc      ag time
1    2300 present   65
2     750 present  156
3    4300 present  100
4    2600 present  134
5    6000 present   16
6   10500 present  108
7   10000 present  121
8   17000 present    4
9    5400 present   39
10   7000 present  143
11   9400 present   56
12  32000 present   26
13  35000 present   22
14 100000 present    1
15 100000 present    1
16  52000 present    5
17 100000 present   65
18   4400  absent   56
19   3000  absent   65
20   4000  absent   17
21   1500  absent    7
22   9000  absent   16
23   5300  absent   22
24  10000  absent    3
25  19000  absent    4
26  27000  absent    2
27  28000  absent    3
28  31000  absent    8
29  26000  absent    4
30  21000  absent    3
31  79000  absent   30
32 100000  absent    4
33 100000  absent   43
moran.test(leuk, ny_nbqw) #except that moran's I can't run on a MODEl, only on a variable
Error: object 'ny_nbqw' not found
coords <- coordinates(ny)
Error: error in evaluating the argument 'obj' in selecting a method for
function 'coordinates': Error: object 'ny' not found
FIPS <- row.names(as(ny, "data.frame"))  # Define the fips code so that I can use K nearest neighbors
Error: object 'ny' not found
lk_kn1 <- knn2nb(knearneigh(coords, k = 1), row.names = FIPS)
Error: object 'coords' not found
lk_kn1_w <- nb2listw(lk_kn1, zero.policy = T)
Error: object 'lk_kn1' not found

#data$lmresid2 <- residuals(pov_lm)  ny$lmresid
lm.morantest(leuk2, lk_kn1_w) # moran I=.9152 p=.18
Error: object 'lk_kn1_w' not found
moran.test(ny$CASES, listw = ny_nbqwb, zero.policy = T) #moran=5.9487 p=1.352e-09
Error: object 'ny_nbqwb' not found



#moran's I on the residuals
ny$resid <-resid(leuk)
Error: object 'ny' not found
plot(moran.mc(ny$resid, ny_nbqw, nsim=999))
Error: error in evaluating the argument 'x' in selecting a method for
function 'plot': Error in inherits(listw, "listw") : object 'ny_nbqw' not
found Calls: moran.mc -> inherits
lm.LMtests(leuk, listw=ny_nbqw, test="all")
Error: object 'ny_nbqw' not found
summary(lm.LMtests(leuk, listw=ny_nbqw, test="all"))
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error in inherits(listw, "listw") : object 'ny_nbqw'
not found Calls: lm.LMtests -> inherits
summary(lm.LMtests(expos, listw=ny_nbqw, test="all"))
Error: error in evaluating the argument 'object' in selecting a method for
function 'summary': Error in lm.LMtests(expos, listw = ny_nbqw, test =
"all") : object 'expos' not found

#lm.morantest(leuk, weights)