Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inverse of 'predict' function

Tags:

r

Using predict() one can obtain the predicted value of the dependent variable (y) for a certain value of the independent variable (x) for a given model. Is there any function that predicts x for a given y?

For example:

kalythos <- data.frame(x = c(20,35,45,55,70), 
    n = rep(50,5), y = c(6,17,26,37,44))
kalythos$Ymat <- cbind(kalythos$y, kalythos$n - kalythos$y)
model <- glm(Ymat ~ x, family = binomial, data = kalythos)

If we want to know the predicted value of the model for x=50:

predict(model, data.frame(x=50), type = "response")

I want to know which x makes y=30, for example.

like image 286
danilinares Avatar asked Nov 16 '10 06:11

danilinares


People also ask

What is inverse prediction?

The object of inverse prediction is to infer the value of a condition x* that caused an observed response y*, based on a linear model relating responses to conditions fit to training data.

What does the predict function return?

Function predict() returns a named list of class Prediction() . Its most important element is $data which is a data. frame that contains columns with the true values of the target variable (in case of supervised learning problems) and the predictions.

What is the predict function?

The predict() function is used to predict the values based on the previous data behaviors and thus by fitting that data to the model. You can also use the confidence intervals to check the accuracy of our predictions. References.


2 Answers

Came across this old thread but thought I would add some other info. Package MASS has function dose.p for logit/probit models. SE is via delta method.

> dose.p(model,p=.6)
             Dose       SE
p = 0.6: 48.59833 1.944772

Fitting the inverse model (x~y) would not makes sense here because, as @VitoshKa says, we assume x is fixed and y (the 0/1 response) is random. Besides, if the data weren’t grouped you’d have only 2 values of the explanatory variable: 0 and 1. But even though we assume x is fixed it still makes sense to calculate a confidence interval for the dose x for a given p, contrary to what @VitoshKa says. Just as we can reparameterize the model in terms of ED50, we can do so for ED60 or any other quantile. Parameters are fixed, but we still calculate CI's for them.

like image 109
davep Avatar answered Sep 19 '22 15:09

davep


Saw the previous answer is deleted. In your case, given n=50 and the model is binomial, you would calculate x given y using:

f <- function (y,m) {
  (logit(y/50) - coef(m)[["(Intercept)"]]) / coef(m)[["x"]]
}
> f(30,model)
[1] 48.59833

But when doing so, you better consult a statistician to show you how to calculate the inverse prediction interval. And please, take VitoshKa's considerations into account.

like image 27
Joris Meys Avatar answered Sep 18 '22 15:09

Joris Meys