Econometria Financeira com o R – OLS e GLM

Análise de Dados Financeiros e Econômicos com o R

Marcelo S. Perlin

EA-UFRGS

12/11/2026

Introduction

Why Financial Econometrics?

The modelling tools from financial econometrics allow the researcher to simulate, predict and test a particular hypothesis about the data.

  • allow correctly testing different hypothesis about the real world
  • models scale well! (likely to be usable for large data)
  • can be very flexible towards a functional form of the data generating process

Please read the references!

Linear Models

Linear Models

A linear model is, without a doubt, one of the most used econometric models in R and finance.

Why??

  • simplicity
  • quick estimation, even for HUGE amounts of data
  • easy to interpret and process out of sample data

A linear model with N explanatory variables can be represented as follows:

\[y _t = \alpha + \beta _1 x_{1,t} + \beta _2 x_{2,t} + ... + \beta _N x_{N,t} + \epsilon _t\]

\(y_t\) – dependent (or response) variable

\(x_{i,t}\) – independent (or explanatory) variables.

\(\beta _i\) – coefficients to be estimated

\(\epsilon _t\) – Residual from linear equation

Simulating a Linear Model

Consider the following equation:

\[y _t = 0.5 + 2 x_{t} + \epsilon _t\]

set.seed(50)

# number of obs
nT <- 1000 

# set x as Normal (0, 1)
x <- rnorm(nT)

# set coefficients
my_alpha <- 0.5
my_beta <- 2

# build y
y <- my_alpha + my_beta*x + rnorm(nT)

library(ggplot2)

# set temp df
temp.df <- dplyr::tibble(x, y)

# plot it
p <- ggplot(temp.df, aes(x = x, y = y)) + 
  geom_point(size = 0.5, alpha = 0.4) +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  geom_abline(intercept = my_alpha, slope = my_beta, 
              color = "blue", linetype = "dashed") +
  labs(x = "x", y = "y",
       title = "Simulated data: true line (blue) vs fitted OLS (red)")

print(p)

Estimating a Linear Model

In R, the main function for estimating a linear model is lm. Let’s use it to estimate a model from the previous simulated data.

# set df
df_reg <- dplyr::tibble(x, y)

# estimate linear model
my_lm <- lm(data = df_reg, formula = y ~ x)

print(my_lm)

Call:
lm(formula = y ~ x, data = df_reg)

Coefficients:
(Intercept)            x  
     0.5083       1.9891  

library(broom)

# tidy table of coefficients with confidence intervals
tidy(my_lm, conf.int = TRUE)
# A tibble: 2 × 7
  term        estimate std.error statistic  p.value conf.low conf.high
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
1 (Intercept)    0.508    0.0311      16.4 1.74e-53    0.447     0.569
2 x              1.99     0.0303      65.6 0           1.93      2.05 

more information about the previously estimated can be found with summary:

print(summary(my_lm))

Call:
lm(formula = y ~ x, data = df_reg)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.0444 -0.6906 -0.0244  0.6807  3.2892 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.50830    0.03107   16.36   <2e-16 ***
x            1.98906    0.03031   65.61   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.9824 on 998 degrees of freedom
Multiple R-squared:  0.8118,    Adjusted R-squared:  0.8116 
F-statistic:  4305 on 1 and 998 DF,  p-value: < 2.2e-16

Reporting a single Linear Model

The typical model table can be constructed in R with texreg or modelsummary:

library(texreg)

htmlreg(my_lm, 
        include.rownames = FALSE,
        caption = "Simulated linear model")
Simulated linear model
  Model 1
(Intercept) 0.51***
  (0.03)
x 1.99***
  (0.03)
R2 0.81
Adj. R2 0.81
Num. obs. 1000
***p < 0.001; **p < 0.01; *p < 0.05

Reporting many models

library(texreg)

# nested models: does the extra flexibility improve the fit?
model_1 <- lm(data = df_reg, formula = y ~ x)
model_2 <- lm(data = df_reg, formula = y ~ x + I(x^2))
model_3 <- lm(data = df_reg, formula = y ~ x + I(x^2) + I(x^3))

htmlreg(l = list(model_1, model_2, model_3), 
        custom.model.names = c("Linear", "Quadratic", "Cubic"),
        include.rownames = FALSE)
Statistical models
  Linear Quadratic Cubic
(Intercept) 0.51*** 0.50*** 0.50***
  (0.03) (0.04) (0.04)
x 1.99*** 1.99*** 1.97***
  (0.03) (0.03) (0.05)
x^2   0.01 0.01
    (0.02) (0.02)
x^3     0.01
      (0.01)
R2 0.81 0.81 0.81
Adj. R2 0.81 0.81 0.81
Num. obs. 1000 1000 1000
***p < 0.001; **p < 0.01; *p < 0.05

A real world example

The table is from published paper titled “The Academic Inbreeding Controversy: Analysis and Evidence from Brazil”, published in Journal of Informetrics

Regression table from the paper on academic inbreeding, showing coefficients and significance stars.

Another real world example

The table is from a working paper titled “The Academia Downturn: the Impact of COVID-19 in the Brazilian Academic Output”

Regression table from the working paper on the impact of COVID-19 on Brazilian academic output, with several model specifications side by side.

Statistical Inference in Linear Models

After estimating a model with function lm, the next step is to test some hypothesis about the coefficients.

  • F-test: verifies the most basic condition for a model to justify its existence – it tests the assumption that all coefficients, excluding the intercept, are equal to zero.

  • T-tests: tests the null hypothesis that individual coefficients (betas) are equal to zero.

  • Adjusted R^2: R^2 penalized by the number of parameters, so it does not automatically increase when irrelevant variables are added.

The F-test in summary()

When function summary is applied to a lm output, the F-test is provided by default in the last line of the text output. The null hypothesis of the test is that all slopes are equal to zero. Let’s try it:

nT <- 100
df <- data.frame(y = runif(nT),
                 x1 = runif(nT),
                 x2 = runif(nT))

my_lm <- lm(data = df, 
            formula = y ~ x1 + x2)

print(summary(my_lm))

Call:
lm(formula = y ~ x1 + x2, data = df)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.51446 -0.26408 -0.01434  0.27508  0.46685 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.5336338  0.0796171   6.703 1.36e-09 ***
x1          -0.0431787  0.1088555  -0.397    0.692    
x2          -0.0003169  0.1136217  -0.003    0.998    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.3015 on 97 degrees of freedom
Multiple R-squared:  0.001626,  Adjusted R-squared:  -0.01896 
F-statistic: 0.07899 on 2 and 97 DF,  p-value: 0.9241

Alternative hypothesis in linear regressions

While T-test calculate the p-values associated with the event of the individual coefficient being equal to zero, we can also design other tests using a Wald Test, such as a particular parameter being equal to a value

As a simple example, let’s test a linear hypothesis for a simulated model. Here, we will create artificial data and test the formal hypothesis that the estimated coefficients are equal to the actual values provided in the simulation.

set.seed(10)

# number of time periods
nT <- 1000

# set parameters
intercept <- 0.5
beta <- 1.5

# simulate
x <- rnorm(nT)
y <- intercept + beta*x + rnorm(nT)

# set df
df <- data.frame(y, x)

# estimate model
my_lm <- lm(data = df, 
            formula = y ~ x )
  • After the estimation of the model, we use function LinearHypothesis from package car (Fox and Weisberg 2019) to implement our formal test.

  • The tested hypotheses are represented using a matrix operation. With this matrix operation, we test the joint hypothesis that the intercept is equal to 0.5 and the slope is equivalent to 1.5.

\[ \underbrace{\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}}_{hypothesis.matrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix} = \underbrace{\begin{bmatrix} 0.5 \\ 1.5 \end{bmatrix} }_{rhs} \]

The actual R code that implements the test is given next.

library(car)

# set test matrix
test_matrix <- matrix(c(intercept,  # alpha test value
                        beta))  # beta test value

# hypothesis matrix 
hyp_mat <- matrix(c(1,0,
                    0,1),nrow = 2)

# do test
my_waldtest <- linearHypothesis(my_lm, 
                                hypothesis.matrix = hyp_mat, 
                                rhs = test_matrix)

# print result
print(my_waldtest)

Linear hypothesis test:
(Intercept) = 0.5
x = 1.5

Model 1: restricted model
Model 2: y ~ x

  Res.Df    RSS Df Sum of Sq      F Pr(>F)
1   1000 1089.1                           
2    998 1086.8  2    2.3766 1.0912 0.3362

As we can see, the test fails to reject the null hypothesis. This means our simulation worked, and the parameters are correctly estimated as expected.

Testing OLS assumptions

Another family of tests commonly applied to linear models is related to its assumptions. Every linear model estimated by OLS assumes:

  1. linearity in the parameters,
  2. independence of the errors,
  3. homoscedasticity (constant variance),
  4. no perfect multicollinearity,
  5. errors approximately Normal (mainly for small-sample inference).
  • If these assumptions are not true, the model may be inefficient or biased, meaning some modification or use of robust estimates is required.

Visual diagnostics

Formal tests are useful, but plots often reveal the problem faster.

par(mfrow = c(1, 2))
plot(my_lm, which = 1:2)
  • Residuals vs fitted: a funnel shape suggests heteroscedasticity.
  • Normal Q-Q: points far from the straight line suggest non-Normal errors.

R packages for testing OLS assumptions

  • package lmtest (Hothorn et al. 2022) tests for independence with the Breusch-Godfrey and Durbin-Watson tests.

  • The Shapiro-Wilk test for normality is available in package stats.

library(lmtest)

# Breusch-Godfrey test - Serial correlation
# Null hypothesis: no serial correlation in residuals
print(bgtest(my_lm, order = 5))

# Breusch-Pagan test - Non-constant variance
# Null hypothesis: homoscedasticity
print(ncvTest(my_lm))

# Durbin-Watson test - Serial correlation
# Null hypothesis: no serial correlation in residuals
print(dwtest(my_lm))

# Shapiro-Wilk test - Normality
# Null hypothesis: data is normally distributed
print(shapiro.test(my_lm$residuals))

    Breusch-Godfrey test for serial correlation of order up to 5

data:  my_lm
LM test = 4.2628, df = 5, p-value = 0.5122

Non-constant Variance Score Test 
Variance formula: ~ fitted.values 
Chisquare = 1.54328, Df = 1, p = 0.21413

    Durbin-Watson test

data:  my_lm
DW = 2.092, p-value = 0.9271
alternative hypothesis: true autocorrelation is greater than 0


    Shapiro-Wilk normality test

data:  my_lm$residuals
W = 0.99803, p-value = 0.2964

As expected, the model with artificial data passed all tests.

dwtest() assumes the residuals are in time order. Here the data is i.i.d., so treat this result as illustrative; we will use the test properly with time series.

Robust standard errors

If heteroscedasticity is present, the OLS point estimates are still unbiased, but the classical standard errors are wrong. A simple fix is to use heteroscedasticity-robust standard errors.

set.seed(123)

n <- 500
x <- runif(n, 1, 10)
y <- 1 + 2*x + rnorm(n, sd = x)   # variance grows with x
df_het <- tibble::tibble(x, y)
lm_het <- lm(y ~ x, data = df_het)

library(sandwich)
library(lmtest)

# classical standard errors
print(coeftest(lm_het))

t test of coefficients:

            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.71554    0.63851  1.1206    0.263    
x            2.07969    0.10594 19.6303   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# heteroscedasticity-robust standard errors (HC1)
print(coeftest(lm_het, vcov = vcovHC(lm_het, type = "HC1")))

t test of coefficients:

            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.71554    0.49563  1.4437   0.1494    
x            2.07969    0.11804 17.6180   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  • The coefficients are identical; only the standard errors, t-statistics and p-values change.
  • The robust version is usually the safer default when the variance of the errors is not constant.

Global validation

Package gvlma (Pena and Slate 2019) provides a top level function that can execute all sorts of tests in linear models, including the ones described before. The main advantage is that it outputs all tests in a single function call.

library(gvlma)

# global validation of model
gvmodel <- gvlma(my_lm) 

# print result
summary(gvmodel)

Call:
lm(formula = y ~ x, data = df)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.2703 -0.6898  0.0063  0.7346  3.8266 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.51510    0.03300   15.61   <2e-16 ***
x            1.54658    0.03329   46.46   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.044 on 998 degrees of freedom
Multiple R-squared:  0.6838,    Adjusted R-squared:  0.6835 
F-statistic:  2159 on 1 and 998 DF,  p-value: < 2.2e-16


ASSESSMENT OF THE LINEAR MODEL ASSUMPTIONS
USING THE GLOBAL TEST ON 4 DEGREES-OF-FREEDOM:
Level of Significance =  0.05 

Call:
 gvlma(x = my_lm) 

                    Value p-value                Decision
Global Stat        3.6404  0.4569 Assumptions acceptable.
Skewness           1.7814  0.1820 Assumptions acceptable.
Kurtosis           0.1738  0.6767 Assumptions acceptable.
Link Function      0.6628  0.4156 Assumptions acceptable.
Heteroscedasticity 1.0224  0.3119 Assumptions acceptable.

Generalized Linear Models (GLM)

Introduction

The generalized linear model (GLM) is a flexible alternative to a linear model. It allows the user to change the distribution of the response and the link function, which specifies how the expected response relates to the linear predictor.

  • GLM is the right choice when the response is not continuous and unbounded: binary (binomial), counts (poisson), or positive continuous (Gamma).

  • Applications of GLM in finance are mostly in credit models, which are used to predict the likelihood of an individual or company to default on its debt.

Specification

We can write a general univariate GLM as:

\[E \left( y _t \right) = g \left(\alpha + \sum ^N _{i=1} \beta _i x_{i,t} \right)\]

Function g() can take many shapes. For example, if we are modelling a binary variable, we can use g() as the logit function:

\[g(x) = \frac{\exp(x)}{1+\exp(x)} \]

  • function g() ensures any value of x will result in a number between 0 and 1.

Why not just use OLS?

With a binary response, OLS can produce predicted values below 0 or above 1, and the constant-variance assumption is violated.

set.seed(42)
n <- 300
x <- runif(n, -3, 3)
p <- plogis(-1 + 3*x)
y <- rbinom(n, 1, p)
df_bin <- tibble::tibble(x, y)

p_ols <- ggplot(df_bin, aes(x, y)) +
  geom_jitter(height = 0.02, alpha = 0.3) +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  labs(title = "OLS on a binary response", y = "y (0/1)")

p_glm <- ggplot(df_bin, aes(x, y)) +
  geom_jitter(height = 0.02, alpha = 0.3) +
  geom_smooth(method = "glm", method.args = list(family = binomial),
              se = FALSE, color = "blue") +
  labs(title = "Logistic regression", y = "y (0/1)")

print(p_ols)
print(p_glm)

  • The OLS line is a straight line and can leave the [0, 1] interval.
  • The logistic curve respects the natural bounds of a probability.

Simulating a GLM Model

As an example, let’s simulate the following GLM model, where the response vector is a Bernoulli variable that takes value 1 with probability \(p_t\). The probabilities are calculated from the non-linear transformation of \(x_t\).

\[p _t = \frac{\exp(2+5x_t)}{1+\exp(2+5x_t)}\]

In R, we use the following code to build the response vector.

set.seed(15)

# set number of obs
n_T <- 500

# set x
x <- rnorm(n_T)

my_alpha <- 2
my_beta <- 5

# set probabilities
z <- my_alpha + my_beta*x
p <- exp(z)/(1+exp(z))

# set response variable
y <- rbinom(n = n_T,
           size = 1, 
           prob = p)

The simulated data

sim_df <- tibble::tibble(x, p, y)

ggplot(sim_df, aes(x = x, y = p)) +
  geom_line(color = "blue", linewidth = 1) +
  geom_jitter(aes(y = y), height = 0.02, alpha = 0.25) +
  labs(x = "x", y = "Probability / outcome",
       title = "The simulated logistic model")
  • The blue curve is the true probability \(p_t = \exp(2 + 5x_t) / (1 + \exp(2 + 5x_t))\).
  • The points are the realized binary outcomes \(y_t \in \{0, 1\}\).

Estimating a GLM Model

  • In R, the estimation of GLM models is accomplished with function stats::glm().

Let’s use the previously simulated data to estimate a logit model:

# estimate GLM
df <- tibble::tibble(x, y)
my_family <- binomial(link = "logit")
my_glm <- glm(data = df, 
              formula = y ~ x , 
              family = my_family)

# print it with summary
print(summary(my_glm))

Call:
glm(formula = y ~ x, family = my_family, data = df)

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   2.1488     0.2622   8.197 2.47e-16 ***
x             4.9050     0.5110   9.598  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 634.18  on 499  degrees of freedom
Residual deviance: 214.14  on 498  degrees of freedom
AIC: 218.14

Number of Fisher Scoring iterations: 7

An example with data

Let’s use a credit card dataset from Kaggle1 to model credit risk.

# read default data
my_f <- afedR3::data_path('CH11_UCI-Credit-Card.csv')
df_default <- readr::read_csv(my_f)

dplyr::glimpse(df_default)
Rows: 30,000
Columns: 25
$ ID                         <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, …
$ LIMIT_BAL                  <dbl> 20000, 120000, 90000, 50000, 50000, 50000, …
$ SEX                        <dbl> 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 2, 2, 2, 1, 1…
$ EDUCATION                  <dbl> 2, 2, 2, 2, 2, 1, 1, 2, 3, 3, 3, 1, 2, 2, 1…
$ MARRIAGE                   <dbl> 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2…
$ AGE                        <dbl> 24, 26, 34, 37, 57, 37, 29, 23, 28, 35, 34,…
$ PAY_0                      <dbl> 2, -1, 0, 0, -1, 0, 0, 0, 0, -2, 0, -1, -1,…
$ PAY_2                      <dbl> 2, 2, 0, 0, 0, 0, 0, -1, 0, -2, 0, -1, 0, 2…
$ PAY_3                      <dbl> -1, 0, 0, 0, -1, 0, 0, -1, 2, -2, 2, -1, -1…
$ PAY_4                      <dbl> -1, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -1, -1, …
$ PAY_5                      <dbl> -2, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1, -1, …
$ PAY_6                      <dbl> -2, 2, 0, 0, 0, 0, 0, -1, 0, -1, -1, 2, -1,…
$ BILL_AMT1                  <dbl> 3913, 2682, 29239, 46990, 8617, 64400, 3679…
$ BILL_AMT2                  <dbl> 3102, 1725, 14027, 48233, 5670, 57069, 4120…
$ BILL_AMT3                  <dbl> 689, 2682, 13559, 49291, 35835, 57608, 4450…
$ BILL_AMT4                  <dbl> 0, 3272, 14331, 28314, 20940, 19394, 542653…
$ BILL_AMT5                  <dbl> 0, 3455, 14948, 28959, 19146, 19619, 483003…
$ BILL_AMT6                  <dbl> 0, 3261, 15549, 29547, 19131, 20024, 473944…
$ PAY_AMT1                   <dbl> 0, 0, 1518, 2000, 2000, 2500, 55000, 380, 3…
$ PAY_AMT2                   <dbl> 689, 1000, 1500, 2019, 36681, 1815, 40000, …
$ PAY_AMT3                   <dbl> 0, 1000, 1000, 1200, 10000, 657, 38000, 0, …
$ PAY_AMT4                   <dbl> 0, 1000, 1000, 1100, 9000, 1000, 20239, 581…
$ PAY_AMT5                   <dbl> 0, 0, 1000, 1069, 689, 1000, 13750, 1687, 1…
$ PAY_AMT6                   <dbl> 0, 2000, 5000, 1000, 679, 800, 13770, 1542,…
$ default.payment.next.month <dbl> 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0…

For now, let’s simplify the analysis and focus on four columns: sex, education, marriage and age. For that, we’ll also need to decode the values in some columns.

library(dplyr)

# read credit card data 
# source: 
# www.kaggle.com/uciml/default-of-credit-card-clients-dataset
# COLUMNS: GENDER: (1 = male; 2 = female). 
#          EDUCATION: 0 = unknown; 1 = graduate school; 
#                     2 = university; 
#                     3 = high school; 
#                     4 = others;
#                     5, 6 = unknown.
#          MARRIAGE: 1 = married; 
#                    2 = single; 
#                    3 = others 
df_default <- df_default |> 
  mutate(default = (default.payment.next.month == 1), 
         D_male_gender = (SEX == 1),
         age = AGE,
         educ = dplyr::recode(as.character(EDUCATION),
                              '0' = 'Unknown',
                              '1' = 'Grad',
                              '2' = 'University', 
                              '3' = 'High School', 
                              '4' = 'Others',
                              '5' = 'Unknown',
                              '6' = 'Unknown'),
         D_marriage = (MARRIAGE == 1)) |>
  select(default, D_male_gender, age, educ, D_marriage) 

glimpse(df_default)
Rows: 30,000
Columns: 5
$ default       <lgl> TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FA…
$ D_male_gender <lgl> FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FAL…
$ age           <dbl> 24, 26, 34, 37, 57, 37, 29, 23, 28, 35, 34, 51, 41, 30, …
$ educ          <chr> "University", "University", "University", "University", …
$ D_marriage    <lgl> TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRU…

Data caveat: 14 observations have EDUCATION = 0 and none of them defaulted. If left as a separate level, they become the reference category and cause complete separation (huge coefficients and standard errors). We recode 0 to Unknown.

Much better! Now we only have the columns of interest, with more appealing names and content. Let’s proceed to the estimation of the glm model.

# estimate glm model
glm_credit <- glm(data=df_default, 
                  formula = default ~ D_male_gender +  age + 
                                      educ + D_marriage,
                  family = binomial(link = "logit"))

# show output
summary(glm_credit)

Call:
glm(formula = default ~ D_male_gender + age + educ + D_marriage, 
    family = binomial(link = "logit"), data = df_default)

Coefficients:
                   Estimate Std. Error z value Pr(>|z|)    
(Intercept)       -1.487666   0.060838 -24.453  < 2e-16 ***
D_male_genderTRUE  0.207892   0.028503   7.294 3.01e-13 ***
age               -0.002417   0.001736  -1.393 0.163731    
educHigh School    0.331518   0.042230   7.850 4.15e-15 ***
educOthers        -1.370155   0.390129  -3.512 0.000445 ***
educUniversity     0.257734   0.031936   8.070 7.01e-16 ***
educUnknown       -1.081717   0.205611  -5.261 1.43e-07 ***
D_marriageTRUE     0.130592   0.031603   4.132 3.59e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 31705  on 29999  degrees of freedom
Residual deviance: 31460  on 29992  degrees of freedom
AIC: 31476

Number of Fisher Scoring iterations: 5

Odds ratios

The coefficients of a logistic model are on the log-odds scale. Exponentiating them gives odds ratios, which are easier to interpret.

library(broom)

tidy(glm_credit, exponentiate = TRUE, conf.int = TRUE) |>
  dplyr::select(term, estimate, conf.low, conf.high, p.value) |>
  dplyr::mutate(dplyr::across(where(is.numeric), \(x) round(x, 3)))
# A tibble: 8 × 5
  term              estimate conf.low conf.high p.value
  <chr>                <dbl>    <dbl>     <dbl>   <dbl>
1 (Intercept)          0.226    0.2       0.254   0    
2 D_male_genderTRUE    1.23     1.16      1.30    0    
3 age                  0.998    0.994     1.00    0.164
4 educHigh School      1.39     1.28      1.51    0    
5 educOthers           0.254    0.107     0.506   0    
6 educUniversity       1.29     1.22      1.38    0    
7 educUnknown          0.339    0.221     0.497   0    
8 D_marriageTRUE       1.14     1.07      1.21    0    
  • An odds ratio above 1 means higher odds of default; below 1 means lower odds.
  • For example, the odds ratio for D_male_gender is approximately 1.23.

Goodness of fit

The R^2 from OLS does not apply to a binary response. Useful alternatives are the McFadden pseudo-R^2, the AIC and the AUC.

# McFadden pseudo-R2
pseudo_r2 <- 1 - as.numeric(logLik(glm_credit) / logLik(update(glm_credit, . ~ 1)))
print(pseudo_r2)
[1] 0.007726326
# AIC (lower is better)
print(AIC(glm_credit))
[1] 31476.39
  • McFadden pseudo-R^2 is usually much smaller than the OLS R^2; values between 0.2 and 0.4 already indicate a very good fit for a binary model.
  • Here it is close to zero, consistent with the low AUC: demographics alone explain little about default.
  • AIC is useful when comparing models estimated on the same data.

From probabilities to decisions

The model outputs a probability, not a yes/no answer. To classify a client, we must choose a threshold.

# predicted probability of default
df_default$p_default <- predict(glm_credit, type = "response")

# classify using a 0.5 threshold
df_default$pred_default <- df_default$p_default > 0.5

# confusion matrix
pred_label <- factor(df_default$pred_default,
                     levels = c(TRUE, FALSE),
                     labels = c("Default", "No default"))
actual_label <- factor(df_default$default,
                       levels = c(TRUE, FALSE),
                       labels = c("Default", "No default"))

table(predicted = pred_label,
      actual = actual_label)
            actual
predicted    Default No default
  Default          0          0
  No default    6636      23364
  • At the 0.5 threshold, this model classifies everyone as “no default”.
  • The default rate is about 22.1%, so a model that always predicts “no default” would already be right most of the time.
  • Accuracy alone is therefore misleading; we need measures such as sensitivity, specificity and AUC.

ROC and AUC

library(pROC)

my_roc <- pROC::roc(response = as.numeric(df_default$default),
                    predictor = df_default$p_default,
                    quiet = TRUE)

print(pROC::auc(my_roc))
Area under the curve: 0.5583
plot(my_roc, print.auc = TRUE, main = "ROC curve - credit default")
  • The ROC curve shows the trade-off between sensitivity and specificity for every possible threshold.
  • AUC = 0.5 is a useless model; AUC = 1 is perfect. Here, AUC is about 0.558.
  • This low AUC is expected: demographic variables alone are weak predictors. Real credit models use payment history, balances and other behavioural variables.

Exercise 1 - Linear Model

  1. Using package yfR, download adjusted price data for ^BVSP and GRND3.SA from 2015 to 2023.

  2. Estimate the beta of GRND3 from the following model:

\[R_t = \alpha + \beta R_{M,t} + \epsilon _t\]

  1. Estimate a new market model with a dummy for negative returns:

\[R_t = \alpha + \beta _1 R_{M,t} + \beta _2 D_t R_{M,t} + \epsilon _t\]

Where \(D_t = 1\) if \(R_{M,t} <0\) and \(D_t = 0\) otherwise

  1. With the first model (without dummies), use package car to test the hypothesis that \(\alpha=0\) and \(\beta = 1\)

  2. Test the underlying assumptions of the previous linear model with package gvlma

Exercise 2 - Linear Models

  1. Using package yfR, download adjusted price data for PETR3, PETR4, GGBR3, GGBR4 from 2010 to 2018.

  2. Using loops, estimate and save the beta of all stocks in a dataframe with two columns (ticker, beta):

\[R_t = \alpha + \beta R_{M,t} + \epsilon _t\]

  1. Report the results of all models with texreg::screenreg in a single table

Exercise 3 - GLM and credit risk

  1. Re-estimate the credit model with age, D_male_gender, educ and D_marriage.

  2. Report the odds ratios and interpret the effects of gender and marriage.

  3. Compute the ROC curve and the AUC of the model.

  4. Compare the AUC with a simpler model that uses only D_male_gender and D_marriage. Does education improve the ranking of clients?

References

Brooks, Chris. 2014. Introductory Econometrics for Finance. Cambridge university press.
Campbell, John Y, Andrew Wen-Chuan Lo, Archie Craig MacKinlay, et al. 1997. The Econometrics of Financial Markets. Vol. 2. Princeton University press Princeton, NJ.
Fox, John, and Sanford Weisberg. 2019. An R Companion to Applied Regression. Third. Sage. https://www.john-fox.ca/Companion/.
Greene, William H. 2003. Econometric Analysis. Pearson Education India.
Hamilton, James Douglas. 1994. Time Series Analysis. Vol. 2. Princeton university press Princeton.
Hothorn, Torsten, Achim Zeileis, Richard W. Farebrother, and Clint Cummins. 2022. Lmtest: Testing Linear Regression Models. https://CRAN.R-project.org/package=lmtest.
Pena, Edsel A., and Elizabeth H. Slate. 2019. Gvlma: Global Validation of Linear Models Assumptions. https://CRAN.R-project.org/package=gvlma.