[R] plotting average effects.

gradstudent nmford at uwm.edu
Fri Oct 21 17:51:09 CEST 2011


i will include the data to read if if you so choose.

dat <- read.dta("http://quantoid.net/hw1_2011.dta")

model in question:

mod99 <- glm(democracy ~ popc100kpc + ngrevpc, data=dat, family=binomial)
------------------

looking for average effects code, with error on mod99.  popckpc is coded in
1k per capita.

> dat3$popc100kpc <- dat$popc100kpc - 100
> dat3$popc100kpc[which(dat3$popc100kpc < min(dat$popc100kpc))] <-
> min(dat$popc100kpc)
> dat2 <- dat3 <- dat
> dat2$popc100kpc <- dat2$popc100kpc + 100
> dat2$popc100kpc[which(dat2$popc100kpc > max(dat$popc100kpc))] <-
> max(dat$popc100kpc)
> dat3$popc100kpc <- dat$popc100kpc - 100
> dat3$popc100kpc[which(dat3$popc100kpc < min(dat$popc100kpc))] <-
> min(dat$popc100kpc)
> pred1 <- predict(mod99, type="response")
> pred2 <- predict(mod99, newdata=dat2, type="response")
> pred3 <- predict(mod99, newdata=dat3, type="response")
----
breaking the variable into groups:

> pop1.group <- cut(dat$popc100kpc, breaks=quantile(dat$popc100kpc,
> seq(0,1,by=.25)), include.lowest=T)
> 	apply, 2, mean)

> means <- by(cbind(pred1, pred2, pred3), list(pop1.group), 
+ 	apply, 2, mean)
> means <- do.call(rbind, means)


----
and finally attempting to plot:

> par(mar=c(7,4,4,2))
> plot(c(1,10), range(c(means)), type="n", xlab="", 
+ 	ylab="Predicted Probability", axes=F)
> arrows(1:10, means[,1], 1:10, means[,2], code=2, length=.1)
> arrows(1:10, means[,1], 1:10, means[,3], code=2, length=.1, col="red")
> points(1:10, means[,1], pch=16)
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
> axis(1, at=1:10, labels=rownames(means), las=2)
Error in axis(1, at = 1:10, labels = rownames(means), las = 2) : 
  'at' and 'labels' lengths differ, 10 != 4
--------


I am not sure how to fix the error.  Thank you for your time.




-----
Ph.D. Candidate
--
View this message in context: http://r.789695.n4.nabble.com/plotting-average-effects-tp3923982p3925945.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list