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

John Thaden jj.thaden at ctral.org
Wed Aug 29 00:30:07 CEST 2012


Hello all,

How do I actually use the output of predict.lm(..., type="terms") to
predict new term values from new response values?

I'm a  chromatographer trying to use R (2.15.1) for one of the most
common calculations in that business:

    - Given several chromatographic peak areas measured for control
samples containing a molecule at known (increasing) concentrations,
      first derive a linear regression model relating the known
concentration (predictor) to the observed peak area (response)
    - Then, given peak areas from new (real) samples containing
unknown amounts of the molecule, use the model to predict
concentrations of the
      molecule in the unknowns.

In other words, given y = mx +b, I need to solve x' = (y'-b)/m for new data y'

and in R, I'm trying something like this

require(stats)
data <- data.frame(area = c(4875, 8172, 18065, 34555), concn = c(25,
50, 125, 250))
new <- data.frame(area = c(8172, 10220, 11570, 24150))
model <- lm(area ~ concn, data)
pred <- predict(model, type = "terms")
#predicts from original data
pred <- predict(model, type = "terms", newdata = new)
                #error
pred <- predict(model, type = "terms", newdata = new, se.fit = TRUE)
          #error
pred <- predict(model, type = "terms", newdata = new, interval =
"prediction")  #error
new2 <- data.frame(area = c(8172, 10220, 11570, 24150), concn = 0)
new2
pred <- predict(model, type = "terms", newdata = new2)
               #wrong results

Can someone please show me what I'm doing wrong?



More information about the R-help mailing list