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

Rui Barradas ruipbarradas at sapo.pt
Wed Aug 29 13:50:15 CEST 2012


Hello,

You seem to be misreading the help pages for lm and predict.lm, argument 
'terms'.
A much simpler way of solving your problem should be to invert the 
fitted model using lm():


model <- lm(area ~ concn, data)  # Your original model
inv.model <- lm(concn ~ area, data = data)  # Your problem's model.

# predicts from original data
pred1 <- predict(inv.model)
# predict from new data
pred2 <- predict(inv.model, newdata = new)

# Let's see it.
plot(concn ~ area, data = data)
abline(inv.model)
points(data$area, pred1, col="blue", pch="+")
points(new$area, pred2, col="red", pch=16)


Also, 'data' is a really bad variable name, it's already an R function.

Hope this helps,

Rui Barradas

Em 28-08-2012 23:30, John Thaden escreveu:
> 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?
>
> ______________________________________________
> 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