In [1]:
%load_ext rpy2.ipython
If the arm library is not installed in your R setup, you'll need to figure out how to install the package. On Ubuntu 14.04 I do something like this:
sudo R
install.packages('arm', dep = TRUE)
Data are in the child.iq directory of the ARM_Data download-- you might have to change the path I use below to reflect the path on your computer.
In [2]:
%%R
# I had to import foreign to get access to read.dta
library("foreign")
kidiq <- read.dta("../../ARM_Data/child.iq/kidiq.dta")
# I won't attach kidiq-- i generally don't attach to avoid confusion(s)
#attach(kidiq)
Loading the arm library -- this produces lots of warnings on the first load that I can't figure out how suppress at this point. However, things work just fine despite the distraction of the warnings. Just rerun the cell to clean up the mess. Also, let me know if you can figure out how get rid of the warnings.
In [10]:
%%R
library("arm")
Fit the regression using the non-jittered data
In [4]:
%%R
fit0 <- lm(kidiq$kid_score ~ kidiq$mom_hs)
display(fit0)
In [5]:
%%R
# -- note that I use kidiq$kid_score because I did not attach kidiq above
kidscore.jitter <- jitter(kidiq$kid_score)
Add some 'binary jitter' to mom_hs
In [6]:
%%R
# define function
jitter.binary <- function(a, jitt=.05){
ifelse (a==0, runif (length(a), 0, jitt), runif (length(a), 1-jitt, 1))
}
# use a slightly different name
mom_hs.jitter <- jitter.binary(kidiq$mom_hs)
Plot using the jittered data
In [7]:
%%R
plot(mom_hs.jitter, kidscore.jitter,
xlab="Mother completed high school",
ylab="Child test score",
pch=20, xaxt="n", yaxt="n")
axis(1, seq(0,1))
axis(2, c(20,60,100,140))
abline(fit0)
In [8]:
%%R
fit1 <- lm (kidiq$kid_score ~ kidiq$mom_iq)
display(fit1)
In [9]:
%%R
plot(kidiq$mom_iq, kidiq$kid_score,
xlab="Mother IQ score",
ylab="Child test score",
pch=20, xaxt="n", yaxt="n")
axis(1, c(80,100,120,140))
axis(2, c(20,60,100,140))
abline (fit1)