且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R中的回归(逻辑):查找特定y值的x值(预测变量)(结果)

更新时间:2022-03-13 09:53:34

从模型计算预测值的最简单方法是使用predict()函数.然后,您可以使用数值求解器查找特定的截距.例如

The easiest way to calculate predicted values from your model is with the predict() function. Then you can use a numerical solver to find particular intercepts. For example

findInt <- function(model, value) {
    function(x) {
        predict(model, data.frame(mpg=x), type="response") - value
     }
}

uniroot(findInt(model, .5), range(mtcars$mpg))$root
# [1] 20.52229

此处findInt仅获取模型和特定目标值,并返回uniroot可以求解为0的函数以找到您的解决方案.

Here findInt just takes the model and a particular target value and returns a function that uniroot can solve for 0 to find your solution.