predict() with arbitrary coefficients in r

前端 未结 3 759
庸人自扰
庸人自扰 2020-12-10 07:51

I\'ve got some coefficients for a logit model set by a non-r user. I\'d like to import those coefficients into r and generate some goodness of fit estimates on the same data

3条回答
  •  鱼传尺愫
    2020-12-10 08:33

    This is not an answer to your posted question - which BondedDust answered - but describes an alternate way in calculating the predicted probabilities yourself which might help in this case.

    # Use the mtcars dataset for a minimum worked example
    data(mtcars)
    
    # Run a logistic regression and get predictions 
    mod <- glm(vs ~ mpg + factor(gear) + factor(am), mtcars, family="binomial")
    p1 <- predict(mod, type="response")
    
    # Calculate predicted probabilities manually
    m <- model.matrix(~ mpg + factor(gear) + factor(am), mtcars)[,]
    p2 <- coef(mod) %*% t(m)
    p2 <- plogis(p2)
    
    all(p1 == p2)
    #identical(as.numeric(p1), as.numeric(p2))
    

    You can replace coef(mod) with the vector of coefficients given to you. model.matrix will generate the dummy variables required for the calculation - check that the ordering is the same as that of the coefficient vector.

提交回复
热议问题