#122 library(Hmisc,T) library(Design,T) describe(counties[,-(1:4)]) #124 older <- counties$age6574 + counties$age75 label(older) <- '% age >= 65, 1990' pdensity <- logb(counties$pop.density+1, 10) label(pdensity) <- 'log 10 of 1992 pop per 1990 miles^2' dd <- datadist(counties) dd <- datadist(dd, older, pdensity) options(datadist='dd') #125 v <- varclus(~ pop.density + pop.change + older + crime + college + income + farm + democrat + republican + Perot + white + turnout, data=counties) plot(v) win.graph() s <- summary(democrat ~ pop.density + pop.change + older + crime + college + income + farm + black + white + turnout, data=counties) plot(s, cex.labels=.7) #127 s <- spearman2(democrat ~ pop.density + pop.change + older + crime + college + income + farm + black + white + turnout, data=counties, p=2) plot(s) #128 f <- ols(democrat ~ rcs(pdensity,4) + rcs(pop.change,3) + rcs(older,3) + crime + rcs(college,5) + rcs(income,4) + rcs(college,5) %ia% rcs(income,4) + rcs(farm,3) + rcs(white,5) + rcs(turnout,3), data=counties) f #130 latex(f) #131 r <- resid(f) xYplot(r ~ fitted(f), method='quantile', nx=200, ylim=c(-10,10), xlim=c(20,60), abline=list(h=0, lwd=.5, lty=2), aspect='fill', data=counties) #132 p1 <-xYplot(r ~ white, method='quantile', nx=200, ylim=c(-10,10), xlim=c(40,100), abline=list(h=0, lwd=.5, lty=2), aspect = 'fill', data=counties) p2 <-xYplot(r ~ pdensity, method='quantile', nx=200, ylim=c(-10,10), xlim=c(0,3.5), abline=list(h=0, lwd=.5, lty=2), aspect = 'fill', data=counties) print (p1, split=c(1,1,1,2), more=T) print (p2, split=c(1,2,1,2)) qqmath(~r | cut2(pdensity, g=4)) #135 f2<- ols(democrat~(rcs(pdensity,4) + rcs(pop.change,3) + rcs(older,3) + crime + rcs(college,5) + rcs(income,4) + rcs(farm,3) + rcs(white,5) + rcs(turnout,3))^2, data=counties) f2$stats #135-136 g<- update(f, x=T) w <- which.influence(g, 0.3) dffits <- resid(g, 'dffits') show.influence(w, data.frame(counties, pdensity, older, dffits), report=c('democrat', 'dffits'), id='county') #136 an<-anova(f) an plot(an, what='partial R2') #Problem in solvet(cov[idx, idx], coef[idx], tol = tol): apparently singular matrix #Use traceback() to see the call stack #138 incomes <- seq(22900, 32800, length=4) show.pts <- function(college.pts, income.pt) { s <- abs(income - income.pt) < 1650 x <- college[s] x <- sort(x[!is.na(x)]) n <- length(x) low <- x[10] high <- x[n-9] college.pts >= low & college.pts <= high } plot(f, college=NA, income=incomes, conf.int=F, xlim=c(0,35), ylim=c(30,55), lty=1, lwd=c(.25,1.5,3.5,6), col=c(1,1,2,2), perim=show.pts) #139 plot(f,ylim=c(20,70)) s<-summary(f) options(digits=4) plot(s) f<- Newlabels(f, list(turnout='voter turnout (%)')) #142 nomogram(f, interact=list(income=incomes), turnout=seq(30,100,by=10), lplabel='estiated % voting Democratic', ce.var=.8, cex.axis=.75)