[R] predict.lm(...,type="terms") question

Rui Barradas ruipbarradas at sapo.pt
Thu Aug 30 14:02:34 CEST 2012


Hello,

Instead of reversing the regression, that, like you say, may have 
problems, it's very easy to wrap the formula

x' <- (y' - beta0)/beta1

in a function and use the direct regression to get new 'x' values from 
new 'y' ones.
This function assumes a first order ols model.

invpredict <- function(model, newdata){
     if(class(newdata) %in% c("data.frame", "list"))
         newdata <- unlist(newdata)
     cc <- coef(model)
     x <- (newdata - cc[1])/cc[2]
     x
}

# Using your last data example
inv2 <- invpredict(model, c(1600, 34000))
cbind(new=c(1600, 34000), inv.pred, inv2)
     new    inv.pred        inv2
1  1600   0.5091916  -0.5980076
2 34000 244.4645607 245.7692308

I wonder what would be the CI's for the predicted 'concn'.

Rui Barradas
Em 30-08-2012 00:08, John Thaden escreveu:
> Draper & Smith sections (3.2, 9.6) address prediction interval issues, but
> I'm also concerned with the linear fit itself. The Model II regression
> literature makes it abundantly clear that OLS regression of x on y
> frequently yields a different line than of y on x. The example below is not
> so extreme, but those given e.g. by Ludbrook, J. (2012) certainly are. Rui
> notes the logical problem of imputing an unknown x using a calibration
> curve where the x values are without error. Regression x on y doesn't help
> that.  But on a practical level, I definitely recall (years ago) using
> predict.lm and newdata to predict x terms. I wish I remembered how.
>
>
> require(stats)
> #Make an illustrative data set
> set.seed(seed = 1111)
> dta <- data.frame(
>      area = c(
>          rnorm(6, mean = 4875, sd = 400),
>          rnorm(6, mean = 8172, sd = 800),
>          rnorm(6, mean = 18065, sd = 1200),
>          rnorm(6, mean = 34555, sd = 2000)),
>      concn = rep(c(25, 50, 125, 250), each = 6))
> model <- lm(area ~ concn, data = dta)
> inv.model <- lm(concn ~ area, data = dta)
> plot(area ~ concn, data = dta)
> abline(model)
> inv.new = cbind.data.frame(area = c(1600, 34000))
> inv.pred <- predict(inv.model, newdata = inv.new)
> lines(x = inv.pred, y = unlist(inv.new), col = "red")
>
> _____________________________
> Ludbrook, J. (2012). "A primer for biomedical scientists on how to execute
> Model II linear regression analysis." Clinical and Experimental
> Pharmacology and Physiology 39(4): 329-335.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list