hatvalues.ols: Get the hat matrix for the OLS

Description Usage Arguments Value Examples

View source: R/rms_SandwichAddon.R

Description

The hat matrix comes from the residual definition:

epsilon = y - Xbeta_hat = (I_n - X(X'X)X')y = (I_n - H)y

where the H is called the hat matrix since

Hy = y_hat

. The hat values are actually the diagonal elements of the matrix that sum up to p (the rank of X, i.e. the number of parameters + 1). See ols.influence().

Usage

1
2
## S3 method for class 'ols'
hatvalues(model, ...)

Arguments

model

The ols model fit

...

arguments passed to methods.

Value

vector

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Generate some data
n <- 500
x1 <- runif(n) * 2
x2 <- runif(n)
y <- x1^3 + x2 + rnorm(n)

library(rms)
library(sandwich)
dd <- datadist(x1, x2, y)
org.op <- options(datadist = "dd")

# Main function
f <- ols(y ~ rcs(x1, 3) + x2)

# Check the bread
bread(f)
# Check the HC-matrix
vcovHC(f, type = "HC4m")
# Adjust the model so that it uses the HC4m variance
f_rob <- robcov_alt(f, type = "HC4m")
# Get the new HC4m-matrix
# - this function just returns the f_rob$var matrix
vcov(f_rob)
# Now check the confidence interval for the function
confint(f_rob)

options(org.op)

Example output

Loading required package: forestplot
Loading required package: grid
Loading required package: magrittr
Loading required package: checkmate
Loading required package: Gmisc
Loading required package: Rcpp
Loading required package: htmlTable
Loading required package: Hmisc
Loading required package: lattice
Loading required package: survival
Loading required package: Formula
Loading required package: ggplot2

Attaching package: 'Hmisc'

The following objects are masked from 'package:base':

    format.pval, units

Loading required package: SparseM

Attaching package: 'SparseM'

The following object is masked from 'package:base':

    backsolve

           Intercept          x1         x1'         x2
Intercept  10.109582 -10.9979835  12.2423215 -6.6267862
x1        -10.997983  23.0793736 -29.4651247  0.2137831
x1'        12.242321 -29.4651247  42.7946815 -0.5232498
x2         -6.626786   0.2137831  -0.5232498 13.4911232
Warning message:
In model.matrix.ols(x) :
  You should set the ols(..., x=TRUE) as the fallback may be somewhat unreliable
            Intercept           x1          x1'           x2
Intercept  0.02319683 -0.026363289  0.029351620 -0.014492635
x1        -0.02636329  0.052794433 -0.066797735  0.001949928
x1'        0.02935162 -0.066797735  0.095471916 -0.003147551
x2        -0.01449264  0.001949928 -0.003147551  0.029646082
Warning messages:
1: In model.matrix.ols(x) :
  You should set the ols(..., x=TRUE) as the fallback may be somewhat unreliable
2: In model.matrix.ols(x) :
  You should set the ols(..., x=TRUE) as the fallback may be somewhat unreliable
3: In model.matrix.ols(x) :
  You should set the ols(..., x=TRUE) as the fallback may be somewhat unreliable
4: In model.matrix.ols(x) :
  You should set the ols(..., x=TRUE) as the fallback may be somewhat unreliable
Warning messages:
1: In model.matrix.ols(x) :
  You should set the ols(..., x=TRUE) as the fallback may be somewhat unreliable
2: In model.matrix.ols(x) :
  You should set the ols(..., x=TRUE) as the fallback may be somewhat unreliable
3: In model.matrix.ols(x) :
  You should set the ols(..., x=TRUE) as the fallback may be somewhat unreliable
4: In model.matrix.ols(x) :
  You should set the ols(..., x=TRUE) as the fallback may be somewhat unreliable
            Intercept           x1          x1'           x2
Intercept  0.02319683 -0.026363289  0.029351620 -0.014492635
x1        -0.02636329  0.052794433 -0.066797735  0.001949928
x1'        0.02935162 -0.066797735  0.095471916 -0.003147551
x2        -0.01449264  0.001949928 -0.003147551  0.029646082
               2.5 %    97.5 %
Intercept -0.1566389  0.440386
x1        -1.1146704 -0.213987
x1'        5.5826734  6.793874
x2         0.8051948  1.480129

Greg documentation built on July 1, 2020, 6:59 p.m.