### load library library("splm") ### set options options(prompt = "R> ", continue = "+ ", width = 70, useFancyQuotes = FALSE) ### readd ata data("Produc", package="Ecdat") data("usaww") ### define formula fm <- log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp ### mat2listw library("spdep") usalw <- mat2listw(usaww) ### spreml1 sararremod <- spml(formula = fm, data = Produc, index = NULL, listw = usalw, model = "random", lag = TRUE, spatial.error = "b") summary(sararremod) ### spreml2 semremod <- spml(formula = fm, data = Produc, index = NULL, listw = usalw, model = "random", lag = FALSE, spatial.error = "kkp") summary(semremod) ### spfeml sararfemod <- spml(formula = fm, data = Produc, index = NULL, listw = usalw, lag = TRUE, spatial.error= "b", model = "within", effect = "individual", method = "eigen", na.action = na.fail, quiet = TRUE, zero.policy = NULL, interval = NULL, tol.solve = 1e-10, control = list(), legacy = FALSE ) summary(sararfemod) ### spfeml sarfemod <- spml(formula = fm, data = Produc, index = NULL, listw = usalw, model="within", effect = "individual", method = "eigen", na.action = na.fail, quiet = TRUE, zero.policy = NULL, interval = NULL, tol.solve = 1e-10, control = list(), legacy = FALSE ) summary(sarfemod) ### spfeml2 eff <- effects(sarfemod) ### spfeml3 semfemod <- spml(formula = fm, data = Produc, listw = usalw,model="within", effect = "time", method = "eigen", na.action = na.fail, quiet = TRUE, zero.policy = NULL, interval = NULL, tol.solve = 1e-10, control = list(), legacy = FALSE ) summary(semfemod) ### spfeml4 eff <- effects(semfemod) eff ### spregm GM_error <- spgm(formula = fm, data = Produc, listw = usaww, moments = "fullweights", model="random", spatial.error = TRUE) summary(GM_error) ### spregm GM_full <- spgm(formula = fm, data = Produc, listw = usaww, moments = "fullweights", model="random", spatial.error = TRUE, lag = TRUE) summary(GM_full) ### spregm GM_error <- spgm(formula = fm, data = Produc, lag = TRUE, listw = usaww, model="within", spatial.error = TRUE) summary(GM_error) ### bsklmtest1 test1<-bsktest(x = fm, data = Produc, listw = mat2listw(usaww), test = "LM1") print(class(test1)) test1 ### bsklmtest2 test2<-bsktest(x = fm, data = Produc, listw = mat2listw(usaww), test = "LM2") test2 ### bsklmtest3 bsktest(x = fm, data = Produc, listw = mat2listw(usaww), test = "CLMlambda") ### sphtest1 test1<-sphtest(x = fm, data = Produc, listw = mat2listw(usaww), spatial.model = "error", method = "GM") test1 ### sphtest2 mod1<- spgm(formula = fm, data = Produc, listw = usaww, moments = "fullweights", model = "random", spatial.error = TRUE, lag = TRUE) mod2<- spgm(formula = fm, data = Produc, listw = usaww, model = "within", spatial.error = TRUE, lag = TRUE) test2<-sphtest(x = mod1, x2 = mod2) test2 ### coeftest library("lmtest") coeftest(sararremod) ### lht library("car") linearHypothesis(sararremod, "log(pcap) = log(pc)") ### initcfr ## read data cigar <- read.table("cigardemo.txt", header = TRUE) ## set formula fm <- logc ~ logp + logpn + logy wcig <- as.matrix(read.table("spat-sym-us.txt")) ## standardization wcig <- wcig/apply(wcig, 1, sum) ## make it a listw lwcig <- mat2listw(wcig) ### feremods ## Fixed effects SAR sarfe <- spml(formula = fm, data = cigar, listw = lwcig, model = "within", effect = "individual", spatial.error = "none", lag = TRUE) ## Fixed effects SEM semfe <- spml(formula = fm, data = cigar, listw = lwcig, model = "within", effect = "individual", spatial.error = "b", lag = FALSE) ## Random effects SAR sarre <- spml(formula = fm, data = cigar, listw = lwcig, model = "random", spatial.error = "none", lag = TRUE) ## Random effects SEM semre <- spml(formula = fm, data = cigar, listw = lwcig, model = "random", spatial.error = "b", lag = FALSE) ### poolmods ## Pooling SAR sarpool <- spml(formula = fm, data = cigar, listw = lwcig, model = "pooling", spatial.error = "none", lag = TRUE) ## Pooling SEM sempool <- spml(formula = fm, data = cigar, listw = lwcig, model = "pooling", spatial.error = "b", lag = FALSE) ## make pooled proximity matrix into listw object pool.lwcig <- mat2listw(kronecker(diag(1,6),listw2mat(lwcig))) ## Pooling SAR by spdep sarpool.2 <- lagsarlm(formula = fm, data = cigar, listw = pool.lwcig) ## Pooling SEM by spdep sempool.2 <- errorsarlm(formula = fm, data = cigar, listw = pool.lwcig)