# function quartplot: A scatterplot for assessing the relationship between # a continuous variable and a dichotomous variable. Includes jitter for # dichotomous variable # OPTIONS: # y = name of a vector containing 0s and 1s # x = name of a vector containing a continous variable # jitamount = amount of jitter above 0 and below 1 # smoothspan = proportion of data used in the lowess smoother # quartiles = 0 or 1: If 1, shows vertical lines at the 25, 50, and 75th # NOTES: # 1. Smoothed line uses real values, not jittered values. # 2. Only y values of 0 and 1 are plotted. Other values ignored. # 3. Quartiles computed among observations where y= 0 or 1. # CREATED by Ken Kleinman, 6/8/2009 quartplot <- function(x,y,jitamount,smoothspan,quantiles) { jittery <- jitter(y, amount=jitamount/2) correction <- ifelse(y==0, jitamount/2, -jitamount/2) jittery <- jittery + correction plot(x,y,type="n") points(x,jittery, pch=20, col="blue") lines(lowess(x,y, f=smoothspan), col = "blue") if (quantiles==1) {abline(v=quantile(x,c(.25,.5,.75)))} } # make some sample data beta <- 0.5 xtest <- rnorm(1000,1,1) linpred <- xtest * beta prob <- exp(linpred)/(1 + exp(linpred)) runis <- runif(1000,0,1) ytest <- ifelse(runis < prob,1,0) quartplot(xtest,ytest,.05,.4,1) quartplot(xtest,ytest,.05,.4,0)