p4.R

kelley — Feb 3, 2014, 8:04 AM

digitize <- FALSE

if (digitize) {
    library(png)
    fig <- readPNG('dS.png')
    par(mar=rep(0,4))
    plot(c(0,1), c(0,1))
    rasterImage(f, 0, 0, 1, 1)
    xaxis <- locator(2) # x axis at 31 and 34
    yaxis <- locator(2) # y axis at -2 and -0.4
    dotsxy <- locator(3) # three dots on graph
} else {
    xaxis <- list(x=c(0.2123444, 0.8360281),
                  y=c(0.1135402, 0.1144546))
    yaxis <- list(x=c(0.1528731, 0.1525534),
                  y=c(0.1132949, 0.7192635))
    dotsxy <- list(x=c(0.4001996,0.4218100,0.6172850),
                   y=c(0.3201958,0.3603612,0.5444846))
}
physical <- function(xy) {
    Saxis <- c(31, 34)
    x <- xaxis$x
    mS <- lm(Saxis ~ x)
    y <- yaxis$y
    daxis <- c(-2, -0.4)
    md <- lm(daxis ~ y)
    list(S = predict(mS, newdata=list(x=xy$x)),
         d = predict(md, newdata=list(y=xy$y)))
}
cat("TEST axes:\n")
TEST axes:
physical(xaxis)$S
 1  2 
31 34 
physical(yaxis)$d
   1    2 
-2.0 -0.4 
dots <- physical(dotsxy)
plot(dots$S, dots$d)
m <- lm(d~S, data=dots)
xrange <- par('usr')[1:2]
SS <- seq(xrange[1], xrange[2], length.out=100)
pred <- predict(m, newdata=list(S=SS), interval="confidence")
lines(cbind(SS, pred[,1]))
lines(cbind(SS, pred[,2]), lty='dashed')
lines(cbind(SS, pred[,3]), lty='dashed')

plot of chunk unnamed-chunk-1

m

Call:
lm(formula = d ~ S, data = dots)

Coefficients:
(Intercept)            S  
    -18.891        0.547  
confint(m)
                 2.5 % 97.5 %
(Intercept) -36.495699 -1.287
S             0.002095  1.093