Análise de Dados Financeiros e Econômicos com o R
EA-UFRGS
12/11/2026
The modelling tools from financial econometrics allow the researcher to simulate, predict and test a particular hypothesis about the data.
Introductory econometrics for finance (Brooks 2014)
Econometric analysis (Greene 2003)
The econometrics of financial markets (Campbell et al. 1997)
Time series analysis (Hamilton 1994)
A linear model is, without a doubt, one of the most used econometric models in R and finance.
Why??
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
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)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.
Call:
lm(formula = y ~ x, data = df_reg)
Coefficients:
(Intercept) x
0.5083 1.9891
# 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:
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
The typical model table can be constructed in R with texreg or modelsummary:
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)| 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 | |||
The table is from published paper titled “The Academic Inbreeding Controversy: Analysis and Evidence from Brazil”, published in Journal of Informetrics
The table is from a working paper titled “The Academia Downturn: the Impact of COVID-19 in the Brazilian Academic Output”
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.
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:
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
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.
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.
Another family of tests commonly applied to linear models is related to its assumptions. Every linear model estimated by OLS assumes:
Formal tests are useful, but plots often reveal the problem faster.
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.
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.
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
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
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.
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.
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.
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)} \]
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)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.
Let’s use the previously simulated data to estimate a logit model:
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
Let’s use a credit card dataset from Kaggle1 to model credit risk.
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 = 0and 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 recode0toUnknown.
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.
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
The coefficients of a logistic model are on the log-odds scale. Exponentiating them gives odds ratios, which are easier to interpret.
# 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
D_male_gender is approximately 1.23.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.
[1] 0.007726326
[1] 31476.39
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
Area under the curve: 0.5583
Using package yfR, download adjusted price data for ^BVSP and GRND3.SA from 2015 to 2023.
Estimate the beta of GRND3 from the following model:
\[R_t = \alpha + \beta R_{M,t} + \epsilon _t\]
\[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
With the first model (without dummies), use package car to test the hypothesis that \(\alpha=0\) and \(\beta = 1\)
Test the underlying assumptions of the previous linear model with package gvlma
Using package yfR, download adjusted price data for PETR3, PETR4, GGBR3, GGBR4 from 2010 to 2018.
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\]
texreg::screenreg in a single tableRe-estimate the credit model with age, D_male_gender, educ and D_marriage.
Report the odds ratios and interpret the effects of gender and marriage.
Compute the ROC curve and the AUC of the model.
Compare the AUC with a simpler model that uses only D_male_gender and D_marriage. Does education improve the ranking of clients?
ADFER - UFRGS