# Single variable in f(z)
par(ask=TRUE)
n = 1000
x <- runif(n,0,2*pi)
x <- sort(x)
z <- runif(n,0,2*pi)
xsq <- x^2
sinx <- sin(x)
cosx <- cos(x)
sin2x <- sin(2*x)
cos2x <- cos(2*x)
ybase1 <- x - .1*xsq + sinx - cosx - .5*sin2x + .5*cos2x
ybase2 <- -z + .1*(z^2) - sin(z) + cos(z) + .5*sin(2*z) - .5*cos(2*z)
ybase <- ybase1+ybase2
sig = sd(ybase)/2
y <- ybase + rnorm(n,0,sig)
# Correct specification for x; z in f(z)
fit <- semip(y~x+xsq+sinx+cosx+sin2x+cos2x,nonpar=~z,window1=.20,window2=.20)
2*fit$df1 - fit$df2
yvect <- c(min(ybase1,fit$xbhat), max(ybase1, fit$xbhat))
xbhat <- fit$xbhat - mean(fit$xbhat) + mean(ybase1)
plot(x,ybase1,type="l",xlab="x",ylab="ybase1",ylim=yvect, main="Predictions for XB")
lines(x, xbhat, col="red")
predse <- sqrt(fit$sig2 + fit$nphat.se^2)
nphat <- fit$nphat - mean(fit$nphat) + mean(ybase2)
lower <- nphat + qnorm(.025)*fit$nphat.se
upper <- nphat + qnorm(.975)*fit$nphat.se
o <- order(z)
yvect <- c(min(lower), max(upper))
plot(z[o], ybase2[o], type="l", xlab="z", ylab="f(z) ",
main="Predictions for f(z) ", ylim=yvect)
lines(z[o], nphat[o], col="red")
lines(z[o], lower[o], col="red", lty="dashed")
lines(z[o], upper[o], col="red", lty="dashed")
rm(list=ls())
# Chicago Housing Sales
data(matchdata)
match05 <- data.frame(matchdata[matchdata$year==2005,])
match05$age <- 2005-match05$yrbuilt
library(maptools)
library(RColorBrewer)
library(akima)
cmap <- readShapePoly(system.file("maps/CookCensusTracts.shp",
package="McSpatial"))
cmap <- cmap[cmap$CHICAGO==1,]
cmap <- cmap[
cmap$CAREA=="Albany Park"|
cmap$CAREA=="Edgewater"|
cmap$CAREA=="Edison Park"|
cmap$CAREA=="Forest Glen"|
cmap$CAREA=="Jefferson Park"|
cmap$CAREA=="Lincoln Square"|
cmap$CAREA=="North Park"|
cmap$CAREA=="Norwood Park"|
cmap$CAREA=="Rogers Park"|
cmap$CAREA=="Uptown"|
cmap$CAREA=="West Ridge", ]
lmat <- coordinates(cmap)
#fixed effects for community areas
fit1 <- lm(lnprice~lnland+lnbldg+rooms+bedrooms+bathrooms+centair+fireplace+brick+
garage1+garage2+age+rr+dcbd +factor(carea), data=match05)
length(fit1$coef)
attach(match05)
xbhat <- fit1$coef[1] + fit1$coef[2]*lnland + fit1$coef[3]*lnbldg +
fit1$coef[4]*rooms + fit1$coef[5]*bedrooms + fit1$coef[6]*bathrooms +
fit1$coef[7]*centair + fit1$coef[8]*fireplace + fit1$coef[9]*brick +
fit1$coef[10]*garage1 + fit1$coef[11]*garage2 + fit1$coef[12]*age +
fit1$coef[13]*rr + fit1$coef[14]*dcbd
lochat <- fitted(fit1) - xbhat
lmean = mean(lochat)
lochat <- interpp(longitude, latitude, lochat, lmat[,1], lmat[,2], extrap=FALSE,
duplicate="mean")$z
cmap$lochat1 <- lochat - lmean + mean(lnprice)
mapplot(cmap,"lochat1",title="Fixed Effects & dcbd")
# nonparametric control for dcbd
fit2 <- semip(lnprice~lnland+lnbldg+rooms+bedrooms+bathrooms+centair+fireplace+brick+
garage1+garage2+ age+rr, nonpar=~dcbd, data=match05)
2*fit2$df1 - fit2$df2
lochat <- fit2$nphat
lmean = mean(lochat)
lochat <- interpp(longitude, latitude, lochat, lmat[,1], lmat[,2], extrap=FALSE,
duplicate="mean")$z
cmap$lochat2 <- lochat - lmean + mean(lnprice)
mapplot(cmap,"lochat2",title="f(dcbd)")
# nonparametric controls for longitude and latitude
fit3 <- semip(lnprice~lnland+lnbldg+rooms+bedrooms+bathrooms+centair+fireplace+brick+
garage1+garage2+ age+rr+dcbd, nonpar=~longitude+latitude, data=match05,
distance="Latlong")
2*fit3$df1 - fit3$df2
lochat <- dcbd*fit3$xcoef["dcbd"] + fit3$nphat
lmean = mean(lochat)
lochat <- interpp(longitude, latitude, lochat, lmat[,1], lmat[,2], extrap=FALSE,
duplicate="mean")$z
cmap$lochat3 <- lochat - lmean + mean(lnprice)
mapplot(cmap,"lochat3",title="f(longitude, latitude) & dcbd")
# Conditional parametric model: y = XB + dcbd*lambda(longitude,latitude) + u
fit4 <- semip(lnprice~lnland+lnbldg+rooms+bedrooms+bathrooms+centair+fireplace+
brick+garage1+garage2+age+rr, nonpar=~longitude+latitude, conpar=~dcbd,
data=match05, distance="Latlong")
2*fit4$df1 - fit4$df2
lochat <- fit4$npfit$yhat
lmean = mean(lochat)
lochat <- interpp(longitude, latitude, lochat, lmat[,1], lmat[,2], extrap=FALSE,
duplicate="mean")$z
cmap$lochat4 <- lochat - lmean + mean(lnprice)
mapplot(cmap,"lochat4",title="dcbd*lambda(longitude, latitude)")
cmap$dcbdhat <- interpp(fit4$npfit$target[,1],fit4$npfit$target[,2],
fit4$npfit$xcoef.target[,"dcbd"],lmat[,1],lmat[,2],duplicate="mean")$z
mapplot(cmap,"dcbdhat",title="dcbd coefficients")
# Conditional parametric model: y = XB + Z*lambda(longitude,latitude) + u
# Z = (dcbd,lnland,lnbldg,age)
fit4 <- semip(lnprice~rooms+bedrooms+bathrooms+centair+fireplace+brick+
garage1+garage2+rr, nonpar=~longitude+latitude, conpar=~dcbd+lnland+lnbldg+age,
data=match05, distance="Latlong")
2*fit4$df1 - fit4$df2
lochat <- fit4$npfit$yhat
lmean = mean(lochat)
lochat <- interpp(longitude, latitude, lochat, lmat[,1], lmat[,2], extrap=FALSE,
duplicate="mean")$z
cmap$lochat4 <- lochat - lmean + mean(lnprice)
mapplot(cmap,"lochat4",title="Nonparametric Part of Model")
cmap$dcbdhat <- interpp(fit4$npfit$target[,1],fit4$npfit$target[,2],
fit4$npfit$xcoef.target[,"dcbd"],lmat[,1],lmat[,2],duplicate="mean")$z
mapplot(cmap,"dcbdhat",title="dcbd coefficients")
cmap$lnlandhat <- interpp(fit4$npfit$target[,1],fit4$npfit$target[,2],
fit4$npfit$xcoef.target[,"lnland"], lmat[,1],lmat[,2],duplicate="mean")$z
mapplot(cmap,"lnlandhat",title="lnland coefficients")
cmap$lnbldghat <- interpp(fit4$npfit$target[,1],fit4$npfit$target[,2],
fit4$npfit$xcoef.target[,"lnbldg"], lmat[,1],lmat[,2],duplicate="mean")$z
mapplot(cmap,"lnbldghat",title="lnbldg coefficients")
cmap$agehat <- interpp(fit4$npfit$target[,1],fit4$npfit$target[,2],
fit4$npfit$xcoef.target[,"age"], lmat[,1],lmat[,2],duplicate="mean")$z
mapplot(cmap,"agehat",title="age coefficients")
Run the code above in your browser using DataLab