How to produce leverage stats?

前端 未结 1 453
故里飘歌
故里飘歌 2021-02-05 06:46

I know how to produce the plots using leveragePlot(), but I can not find a way to produce a statistic for leverage for each observation like in megastat output.

1条回答
  •  花落未央
    2021-02-05 07:05

    I think you're looking for the hat values.

    Use hatvalues(fit). The rule of thumb is to examine any observations 2-3 times greater than the average hat value. I don't know of a specific function or package off the top of my head that provides this info in a nice data frame but doing it yourself is fairly straight forward. Here's an example:

    fit <- lm(hp ~ cyl + mpg, data=mtcars) #a fake model
    
    hatvalues(fit)
    
    hv <- as.data.frame(hatvalues(fit))
    mn <-mean(hatvalues(fit))
    hv$warn <- ifelse(hv[, 'hatvalues(fit)']>3*mn, 'x3',
       ifelse(hv[, 'hatvalues(fit)']>2*mn, 'x3', '-' ))
    
    hv
    

    For larger data sets you could use subset and/or orderto look at just certain values ranges for the hat values:

    subset(hv, warn=="x3")
    subset(hv, warn%in%c("x2", "x3"))
    hv[order(hv['hatvalues(fit)']), ]
    

    I actually came across a nice plot function that does this in the book R in Action but as this is a copyrighted book I will not display Kabacoff's intellectual property. But that plot would work even better for mid sized data sets.

    Here is a decent hat plot though that you may also want to investigate:

    plot(hatvalues(fit), type = "h")
    

    0 讨论(0)
提交回复
热议问题