Type parameter of the predict() function

后端 未结 3 1452
青春惊慌失措
青春惊慌失措 2021-02-05 08:29

What is the difference between type=\"class\" and type=\"response\" in the predict function?

For instance between:



        
3条回答
  •  余生分开走
    2021-02-05 08:50

    see ?predict.lm: predict.lm produces a vector of predictions or a matrix of predictions and bounds with column names fit, lwr, and upr if interval is set. For type = "terms" this is a matrix with a column per term and may have an attribute "constant".

    > d <- data.frame(x1=1:10,x2=rep(1:5,each=2),y=1:10+rnorm(10)+rep(1:5,each=2))
    > l <- lm(y~x1+x2,d)
    > predict(l)
            1         2         3         4         5         6         7         8         9        10 
     2.254772  3.811761  4.959634  6.516623  7.664497  9.221486 10.369359 11.926348 13.074222 14.631211 
    
    > predict(l,type="terms")
               x1         x2
    1  -7.0064511  0.8182315
    2  -5.4494620  0.8182315
    3  -3.8924728  0.4091157
    4  -2.3354837  0.4091157
    5  -0.7784946  0.0000000
    6   0.7784946  0.0000000
    7   2.3354837 -0.4091157
    8   3.8924728 -0.4091157
    9   5.4494620 -0.8182315
    10  7.0064511 -0.8182315
    attr(,"constant")
    [1] 8.442991
    

    i.e. predict(l) is the row sums of predict(l,type="terms") + the constant

提交回复
热议问题