### R code from vignette source 'Lecture2_2015.Rnw' ### Encoding: UTF-8 ################################################### ### code chunk number 1: eurodist ################################################### load("euro_cities.rda") library(smacof) s <- smacofSym(euro_cities) plot(-s$conf, type="n") text(-s$conf[,1], -s$conf[,2], rownames(s$conf)) ################################################### ### code chunk number 2: plotthreeutilityfunctions1 ################################################### norm.util <- function(x){ return(15*exp(-0.25*(0-x)^2))} quad.util <- function(x){ return(15+15*(-0.25*(0-x)^2))} lin.util <- function(x){ return(5*-abs(x)+15)} plot(c(-3,3),c(0,20),type="n",bty="n",xlab="Policy Location",ylab="Utility",cex.lab=1.2) lines(seq(-3,3,0.01), norm.util(seq(-3,3,0.01)),lwd=3,lty=1) lines(seq(-3,3,0.01), quad.util(seq(-3,3,0.01)),lwd=3,lty=2) lines(seq(-3,3,0.01), lin.util(seq(-3,3,0.01)),lwd=3,lty=3) ################################################### ### code chunk number 3: franceread ################################################### u <- url("http://www.quantoid.net/files/essex/franceees2009.rda") load(u) close(u) ################################################### ### code chunk number 4: francereadlocal (eval = FALSE) ################################################### ## load("franceEES2009.rda") ################################################### ### code chunk number 5: aldmck2 ################################################### library(basicspace) result <- aldmck(franceEES2009, respondent=1, polarity=2, missing=c(77,88,89), verbose=FALSE) ################################################### ### code chunk number 6: sumaldmck ################################################### summary(result) ################################################### ### code chunk number 7: resps ################################################### voters <- na.omit(result$respondents) head(voters) ################################################### ### code chunk number 8: box1 ################################################### boxplot(idealpt ~ factor(selfplace), data=voters, xlab = "Original Self-placement", ylab="Ideal Points") ################################################### ### code chunk number 9: distip ################################################### source("http://www.quantoid.net/files/essex/aldmck_functions.r") pl <- plot2.aldmck(result, bw=.2) update(pl, ylim=c(-.05, 1.25)) pl ################################################### ### code chunk number 10: distip2 ################################################### pl <- plot2.aldmck(result, bw=.2, col=c("gray33", "gray66"), pch=15:18) update(pl, ylim=c(-.05, 1.25)) pl ################################################### ### code chunk number 11: distip_sign ################################################### resps <- na.omit(result$respondents) signw <- factor(sign(resps$weight), levels=c(-1,1), labels=c("Negative", "Positive")) pl <- plot2.aldmck(result, bw=.05, by = "signw", col=c("gray33", "gray66"), pch=15:18) pl ################################################### ### code chunk number 12: boot_noeval (eval = FALSE) ################################################### library(boot) out <- boot.aldmck(franceEES2009, respondent=1, missing=c(77,88,89), polarity=2, R=1500, ci="perc") ################################################### ### code chunk number 13: boot_read ################################################### # library(boot) # u <- url("http://www.quantoid.net/files/essex/amboot.rda") # load(u) # close(u) ################################################### ### code chunk number 14: bootci ################################################### ci.out <- ci.aldmck(out, ci="perc") ci.out$ci ################################################### ### code chunk number 15: ciplot ################################################### plot.baldmck(ci.out, cex=.5, xlab="Ideal Points") ################################################### ### code chunk number 16: testdiffs ################################################### test.baldmck(out) ################################################### ### code chunk number 17: readcds ################################################### u <- url("http://www.quantoid.net/files/essex/cds2000.rda") load(u) close(u) head(CDS2000[,5:8]) issues <- as.matrix(CDS2000[,5:14]) ################################################### ### code chunk number 18: bbcds ################################################### result <- blackbox(issues, missing=99, dims=3, minscale = 5, verbose=F) result$fits ################################################### ### code chunk number 19: 1dbb ################################################### result$stimuli[[1]] ################################################### ### code chunk number 20: 1dbb ################################################### result$stimuli[[2]] ################################################### ### code chunk number 21: 1dbb ################################################### result$stimuli[[3]] ################################################### ### code chunk number 22: bbplot ################################################### party <- factor(CDS2000[,1], levels=c(1,2), labels=c("Democrat", "Republican")) plot.bb(result, issues, by="party", col=c("gray33", "gray66"), pch=c("D", "R"), rug=T, cex=.5, nv=TRUE, xlim=c(-1,1), ylim=c(-1,1)) ################################################### ### code chunk number 23: bbdim1 ################################################### x <- result$individuals[[2]][,1] densityplot(x, groups=party, pch=NA, auto.key=list(space="top")) ################################################### ### code chunk number 24: bbt ################################################### u1 <- url("http://www.quantoid.net/files/essex/mexicocses2000.rda") u2 <- url("http://www.quantoid.net/files/essex/mexicocses2006.rda") load(u1) load(u2) close(u1) close(u2) ################################################### ### code chunk number 25: dobbt (eval = FALSE) ################################################### result_2000 <- blackbox_transpose(mexicoCSES2000, missing=99, dims=3, minscale=5, verbose=TRUE) result_2006 <- blackbox_transpose(mexicoCSES2006, missing=99, dims=3, minscale=5, verbose=TRUE) ################################################### ### code chunk number 26: readbbt ################################################### # u <- url("http://www.quantoid.net/files/essex/mexico_bbt.rda") # load(u) # close(u) ################################################### ### code chunk number 27: reflect ################################################### x2000 <- -1 * result_2000$stimuli[[2]][,2] y2000 <- result_2000$stimuli[[2]][,3] x2006 <- -1 * result_2006$stimuli[[2]][,2] y2006 <- result_2006$stimuli[[2]][,3] ################################################### ### code chunk number 28: BST_Mexico2000plot ################################################### plot(x2000, y2000, main="2000 Stimuli Locations", xlab=paste("First Dimension: ", round(result_2000$fits[1,3],2), "%", sep=""), ylab=paste("Second Dimension: ", round(result_2000$fits[2,3],2), "%", sep=""), xlim=c(-1,1), ylim=c(-1,1), asp=1, type="n") points(x2000, y2000, pch=16, font=2, col="black") text(x2000, y2000, colnames(mexicoCSES2000), pos=c(4,4,4,4,4,2), offset=0.40) ################################################### ### code chunk number 29: BST_Mexico2006plot ################################################### plot(x2006, y2006, main="2006 Stimuli Locations", xlab=paste("First Dimension: ", round(result_2006$fits[1,3],2), "%", sep=""), ylab=paste("Second Dimension: ", round(result_2006$fits[2,3],2), "%", sep=""), xlim=c(-1,1), ylim=c(-1,1), asp=1, type="n") points(x2006, y2006, pch=16, font=2) text(x2006, y2006, colnames(mexicoCSES2006), pos=c(4,4,1,4,4,4,2,3), offset=0.40)