Title: | Linear Time-Varying Coefficient Models |
---|---|
Description: | Convert linear model to a time-varying coefficient model using stepwise regressions, local regressions or state-space models. |
Authors: | Claire de Rosamel [aut], Alain Quartier-la-Tente [aut, cre] |
Maintainer: | Alain Quartier-la-Tente <[email protected]> |
License: | EUPL |
Version: | 0.2.1 |
Built: | 2024-11-01 11:24:35 UTC |
Source: | https://github.com/InseeFrLab/tvCoef |
Computes as many regressions as breakup dates within a global model
bp_lm(x, left = TRUE, break_dates, tvlm = FALSE, ...)
bp_lm(x, left = TRUE, break_dates, tvlm = FALSE, ...)
x |
|
left |
|
break_dates |
optional, to indicate the breakup dates if they are known. |
tvlm |
By default set to |
... |
other arguments passed to |
Returns an element of class bp_lm
. It is a list containing the following elements:
model |
all computed models, each of class |
start |
start date of the time serie |
end |
end date of the time serie |
frequency |
frequency of the time serie |
breakdates |
a list of the breakup dates |
left |
same as the parameter specified above |
tvlm |
same as the parameter specified above |
Splits a database according to one (or more) date
break_data(x, break_dates, left = TRUE, names = NULL, ...)
break_data(x, break_dates, left = TRUE, names = NULL, ...)
x |
a |
break_dates |
the date(s) at which you want to divide the data |
left |
|
names |
optional vector containing the names of the variables used to build the splitted data.
By default the function try to guess the names from the |
... |
other unused arguments |
a mts
containing as many times more data columns than breakdates
Extract Full Transformed Exogoneous matrix
full_exogeneous_matrix(model, ...)
full_exogeneous_matrix(model, ...)
model |
the model |
... |
other unused parameters |
Dataset containing the quarterly growth of the total gross domestic product (GDP) of France and quarterly series of the French business climate in level and in difference.
gdp
gdp
A quarterly ts
object from 1949Q2 to 2024Q1.
Dataset containing the quarterly growth of the total gross domestic product (GDP, "growth_gdp"
) of France,
in volumes chained at previous year prices, seasonally and working day adjusted;
and the French business climate in level.
The French business climate is a monthly series, it is transformed into three quarterly series using the month's place in the quarter.
For example, "bc_fr_m1"
contains the values in the first month of each quarter, and the "diff_fr_m1"
is the difference
of the previous variable (the 2000Q1 value corresponds to the difference in business climate between January 2000 and October 1999).
Data were downloaded March 15, 2024 and might therefore differ from the latest available data.
INSEE
Retrieves the data used in the model
get_data(model, ...) ## S3 method for class 'lm' get_data(model, start = 1, frequency = 1, ...) ## S3 method for class 'dynlm' get_data(model, ...) ## S3 method for class 'tvlm' get_data(model, end = numeric(), frequency = 1, ...) ## S3 method for class 'bp_lm' get_data(model, ...) ## S3 method for class 'piece_reg' get_data(model, ...)
get_data(model, ...) ## S3 method for class 'lm' get_data(model, start = 1, frequency = 1, ...) ## S3 method for class 'dynlm' get_data(model, ...) ## S3 method for class 'tvlm' get_data(model, end = numeric(), frequency = 1, ...) ## S3 method for class 'bp_lm' get_data(model, ...) ## S3 method for class 'piece_reg' get_data(model, ...)
model |
the model |
... |
other unused parameters. |
start |
the start of the data. |
frequency |
the frequency of the data. |
end |
the end of the data. |
Extract Formula From model
get_formula(x) ## Default S3 method: get_formula(x)
get_formula(x) ## Default S3 method: get_formula(x)
x |
the model. |
Get elements of rmse_prev
get_lm_coef(model) get_tvlm_coef(model, date, variable) get_tvlm_bw(model) get_rmse_bw_small(model, ...)
get_lm_coef(model) get_tvlm_coef(model, date, variable) get_tvlm_bw(model) get_rmse_bw_small(model, ...)
model |
result of rmse_prev fonction |
date |
the date on which we want the tvlm coefficients |
variable |
|
... |
other unused parameters |
get_lm_coeff
allows to get all coefficients of all linear regression prediction models
get_tvlm_coeff
allows to get all coefficients of the variable at a certain date of all local regression prediction models
get_tvlm_bw
allows to get the bandwidth of all local regression prediction models
get_rmse_bw_small
allows to get rmse of linear, local and piecewise regression prediction models, when the bandwidth of the prediction model is different from 20.
get_coeff_plot
plot get_tvlm_coeff
of a certain variable at a certain date, get_lm_coeff
of the same variable and the bandwidth of all prediction models, thanks to get_tvlm_bw
. It also highlights when the bandwidth is equal to 20.
Hansen Table
hansen_table
hansen_table
An object of class data.frame
with 20 rows and 7 columns.
Hansen, Bruce E. 1990. "Lagrange multiplier tests for parameter instability in non-linear models". University of Rochester. https://users.ssc.wisc.edu/~bhansen/papers/LMTests.pdf.
Performs Hansen test
hansen_test(x, var, sigma = FALSE)
hansen_test(x, var, sigma = FALSE)
x |
|
var |
variables used for the joint test. By default all the variable are used. |
sigma |
|
Perform Hansen test, which indicates if the variance of a model, a global model and the coefficients of the variable within this model are likely to be unstable over time.
HO: the coefficient/model is stable over time.
Bruce E Hansen "Testing for parameter instability in linear models". Journal of policy Modeling (1992)
model_gdp <- lm( formula = growth_gdp ~ bc_fr_m1 + diff_bc_fr_m1, data = gdp ) hansen_test(model_gdp)
model_gdp <- lm( formula = growth_gdp ~ bc_fr_m1 + diff_bc_fr_m1, data = gdp ) hansen_test(model_gdp)
Check if model has intercept
has_intercept(x)
has_intercept(x)
x |
a model |
Get last coefficients of lm or tvLM models.
last_coef(x)
last_coef(x)
x |
a 'tvlm' or 'lm' object |
Fixed Window Regression
lm_fenetre_fixe(formula, data, nbw = 1)
lm_fenetre_fixe(formula, data, nbw = 1)
formula |
a |
data |
time series data. |
nbw |
number of windows. |
Return an object of class "lmffixe". Return all models, from which we can extract the usual coefficients, residuals, and fitted.values. And the divisor chosen by the function (arbitrary the middle one), the period, i.e. the length of each sub models, and the frequency of the data.
Computes different types of regressions with some coefficients fixed and others allowed to vary
lm_fixed_coeff(formula, data, fixed_var, ...)
lm_fixed_coeff(formula, data, fixed_var, ...)
formula |
a |
data |
time series data. |
fixed_var |
chosen variables whose coefficients aren't allowed to vary through time |
... |
further arguments passed to |
global_model |
the simple |
linear_reg |
simple |
piecewise_reg |
|
tv_reg |
|
Checks there is no residual effect in a model
lm_residual_effect(x, var = c(-1))
lm_residual_effect(x, var = c(-1))
x |
|
var |
the variables on which the residuals are to be regressed. By default use them all and cancel the explained variable |
data("AirPassengers") model <- lm(AirPassengers ~ time(AirPassengers)) lm_residual_effect(model)
data("AirPassengers") model <- lm(AirPassengers ~ time(AirPassengers)) lm_residual_effect(model)
Dataset containing the quarterly growth of production in the manufacturing sector and its main sub-sectors, quarterly balance of opinion of business surveys published by INSEE and Banque de France and quarterly overhang of the industrial production index.
manufacturing
manufacturing
A quarterly ts
object from 1949Q2 to 2024Q1.
Dataset containing the quarterly growth of production in the manufacturing sector and its main sub-sectors and quarterly series series of business surveys published by INSEE and Banque de France.
The sectors studied are:
Manufacturing industry
Food products and beverages (C1)
Capital goods (C3)
Transport equipments (C4)
Other manufacturing (C5)
"manuf_prod"
contains the quarterly growth in production in the manufacturing sector,
and the sub-sectors are in the form "prod_c1"
, "prod_c3"
, "prod_c4"
and "prod_c5"
.
The overhang of the industrial production index corresponds to the quarterly growth obtained extending the series by the last known value:
"overhang_ipi0"
is the quarterly growth obtained extending the series by the last value of the previous quarter (December, March, June, September);
"overhang_ipi1"
is the quarterly growth obtained extending the series by the first value of the current quarter (January, April, July, October);
"overhang_ipi2"
is the quarterly growth obtained extending the series by the second value of the current quarter (February, May, August, November).
The business surveys being monthly, the balance of opinion are transformed into three quarterly series using the month's place in the quarter (for example taking the values of January, April, July and October). Variable names are constructed as the combination of several codes defined as follows:
data source code (INSEE, ins
, or Banque de France, bdf
);
name of the balance of opinion:
Code | Definition |
bc | Business climate |
oscd | Overall order books |
tppa and prodpas | Past production |
tppre and prodpre | Personal production expectation |
sitcar | Situation of order books |
evocar | Evolution of order books |
prix | Selling prices |
stocks | Inventories of finished goods |
tres | Cash position |
tuc | Capacity utilisation rate |
sector: nothing for the manufacturing industri and "c1"
, "c3"
, "c4"
or "c5"
for the sub-sectors;
place of the month in the quarter: m1
, m2
or m3
for the first, second or third month of the quarter.
The dataset also contains some dummies labelled "indYYYYQX"
, where YYYY
is the year and X
is the quarter.
INSEE, Banque de France
Functions to test if any coefficient is fixed or moving according to the Hansen test (hansen_test()
)
moving_coefficients( x, a = c(5, 1, 2.5, 7.5, 10, 20), sigma = FALSE, intercept = TRUE ) fixed_coefficients( x, a = c(5, 1, 2.5, 7.5, 10, 20), sigma = FALSE, intercept = TRUE )
moving_coefficients( x, a = c(5, 1, 2.5, 7.5, 10, 20), sigma = FALSE, intercept = TRUE ) fixed_coefficients( x, a = c(5, 1, 2.5, 7.5, 10, 20), sigma = FALSE, intercept = TRUE )
x |
|
a |
level |
sigma |
|
intercept |
boolean indicating if the intercept should be consider as a moving coefficient when at least one other variable is moving. |
NULL
if no variable selected, otherwise the order of the variables.
Out of sample forecast (or simulated out of sample)
oos_prev(model, date = 28, period = 1, ...) ## S3 method for class 'lm' oos_prev(model, date = 28, period = 1, data = NULL, ...) ## S3 method for class 'piece_reg' oos_prev(model, date = 28, period = 1, ...) ## S3 method for class 'tvlm' oos_prev( model, date = 28, period = 1, data_est = NULL, fixed_bw = FALSE, bw = NULL, end = numeric(), frequency = 1, ... ) ## S3 method for class 'bp_lm' oos_prev( model, date = 28, period = 1, data_est = NULL, data, fixed_bw = FALSE, bw = NULL, ... ) ## S3 method for class 'piece_reg' oos_prev(model, date = 28, period = 1, ...)
oos_prev(model, date = 28, period = 1, ...) ## S3 method for class 'lm' oos_prev(model, date = 28, period = 1, data = NULL, ...) ## S3 method for class 'piece_reg' oos_prev(model, date = 28, period = 1, ...) ## S3 method for class 'tvlm' oos_prev( model, date = 28, period = 1, data_est = NULL, fixed_bw = FALSE, bw = NULL, end = numeric(), frequency = 1, ... ) ## S3 method for class 'bp_lm' oos_prev( model, date = 28, period = 1, data_est = NULL, data, fixed_bw = FALSE, bw = NULL, ... ) ## S3 method for class 'piece_reg' oos_prev(model, date = 28, period = 1, ...)
model |
an object used to select a method |
date |
choose when we want to start the revision process after the start date. By default set to 28 periods. |
period |
choose by how many values we want to move forward. By default set to 1. |
... |
other arguments |
data |
a |
data_est , end , frequency
|
optional arguments to specify the data used to estimate the model, the last date and the frequency |
fixed_bw |
|
bw |
bandwidth of the local regression (when |
oos_prev returns an object of class revision
, only for models of class lm and tvlm. For an object of class bplm
it returns the same forecasts and residuals as below.
An object of class revision
is a list containing the following elements:
model |
all models used to forecast |
debut |
same as date chosen earlier |
intervalle |
same as period chosen earlier |
end_dates |
a vector of all end date of each models |
frequency |
the frequency of the data |
forecast |
the forecast |
residuals |
the errors of the forecast |
data_gdp <- window(gdp, start = 1980, end = c(2019, 4)) reg_lin <- lm( formula = growth_gdp ~ bc_fr_m1 + diff_bc_fr_m1, data = data_gdp ) oos <- oos_prev(reg_lin)
data_gdp <- window(gdp, start = 1980, end = c(2019, 4)) reg_lin <- lm( formula = growth_gdp ~ bc_fr_m1 + diff_bc_fr_m1, data = data_gdp ) oos <- oos_prev(reg_lin)
Computes one global linear regression, on splitted data
piece_reg( x, break_dates = NULL, fixed_var = NULL, tvlm = FALSE, bw = NULL, left = TRUE, ... )
piece_reg( x, break_dates = NULL, fixed_var = NULL, tvlm = FALSE, bw = NULL, left = TRUE, ... )
x |
|
break_dates |
optional, to indicate the breakdates if they are known. By default set to |
fixed_var |
fixed variables (not splitted using |
tvlm |
By default set to |
bw |
bandwidth of the local regression (when |
left |
|
... |
other arguments passed to |
Computes possible breakdates if not filled in. Uses function break_data and run a linear regression on the same splitted data.
Returns an element of class lm
According to parameter fixed_var
, computes a new explained variable, which is the explained variable minus the product between estimated coefficients and values of the fixed variables.
resid_lm_fixed(x, fixed_var)
resid_lm_fixed(x, fixed_var)
x |
|
fixed_var |
list of variables that don't vary through time according to hansen_test |
A new environment where the explained variable is named "fixed".
Root mean squarred error
rmse(resid)
rmse(resid)
resid |
the residuals vector on which rmse will be calculated |
Computes 6 models: linear regression, piecewise regression (with linear regression and local regression), local regression (with tvLM), piecewise regression with some fixed coefficients and tvlm with fixed coefficients. Computes 6 rmse on their residuals and 6 others on the residuals of the predictions of these models.
rmse_prev(x, data, fixed_var = NULL, fixed_bw = FALSE, ...) ## S3 method for class 'formula' rmse_prev(x, data, fixed_var = NULL, fixed_bw = FALSE, ...) ## S3 method for class 'lm' rmse_prev(x, data, fixed_var = NULL, fixed_bw = FALSE, ...)
rmse_prev(x, data, fixed_var = NULL, fixed_bw = FALSE, ...) ## S3 method for class 'formula' rmse_prev(x, data, fixed_var = NULL, fixed_bw = FALSE, ...) ## S3 method for class 'lm' rmse_prev(x, data, fixed_var = NULL, fixed_bw = FALSE, ...)
x |
an object of class |
data |
a |
fixed_var |
which variables of the model should have their coefficients fixed for the models with fixed coefficients. Obtained thanks to the hansen test. |
fixed_bw |
|
... |
additional arguments |
In additional arguments, parameters as date and period can be informed. As in oos_prev they are by default respectively set to 28 and 1.
To estimate forecast models, the function oos_prev is used.
For the forecasts of the two models with fixed coefficients, fixed coefficients are re-estimated at each date, before bp_lm or tvLM are run on moving variables.
Returns an object of class prev
which is a list containing the following elements:
model |
a list of the 6 explanatory models |
forecast |
a list of the 6 predictions of the 6 previous models |
rmse |
a list of the 2 computed rmse, in sample and out of sample |
Computes state space model with one equation. Starting with a simple lm
model, build the all state space model and run it.
Use rjd3sts
packages.
ssm_lm( x, trend = FALSE, var_intercept = 0, var_slope = 0, var_variables = 0, fixed_var_intercept = TRUE, fixed_var_trend = TRUE, fixed_var_variables = TRUE, ..., remove_last_dummies = FALSE, intercept = TRUE )
ssm_lm( x, trend = FALSE, var_intercept = 0, var_slope = 0, var_variables = 0, fixed_var_intercept = TRUE, fixed_var_trend = TRUE, fixed_var_variables = TRUE, ..., remove_last_dummies = FALSE, intercept = TRUE )
x |
a |
trend |
boolean indicating if the model should have a trend. |
var_intercept , var_slope
|
variance of the intercept (used if |
var_variables |
variance of the other variables: can be either a single value (same variance for all the variables) or a vector specifying each variance. |
fixed_var_intercept , fixed_var_trend , fixed_var_variables
|
logical indicating if the variance are fixed or estimated. |
... |
other arguments used in |
remove_last_dummies |
boolean indicating if current dummies (i.e.: only 0 and 1 at the last date) should be removed. |
intercept |
boolean indicating if the model should have an intercept. |
Returns a list
containing:
smoothed_states |
|
smoothed_stdev |
|
filtering_states |
|
filtering_stdev |
|
parameters |
some estimation parameters |
data |
data used in the original model |
data_gdp <- window(gdp, start = 1980, end = c(2019, 4)) reg_lin <- lm( formula = growth_gdp ~ bc_fr_m1 + diff_bc_fr_m1, data = data_gdp ) ssm <- ssm_lm(reg_lin, fixed_var_intercept = FALSE, fixed_var_variables = FALSE) ssm summary(ssm)
data_gdp <- window(gdp, start = 1980, end = c(2019, 4)) reg_lin <- lm( formula = growth_gdp ~ bc_fr_m1 + diff_bc_fr_m1, data = data_gdp ) ssm <- ssm_lm(reg_lin, fixed_var_intercept = FALSE, fixed_var_variables = FALSE) ssm summary(ssm)
Computes out of sample forecasts of a given state space model. Unlike ssm_lm it can manage dummies.
ssm_lm_oos( x, trend = FALSE, var_intercept = 0, var_slope = 0, var_variables = 0, fixed_var_intercept = TRUE, fixed_var_trend = TRUE, fixed_var_variables = TRUE, date = 28, ... )
ssm_lm_oos( x, trend = FALSE, var_intercept = 0, var_slope = 0, var_variables = 0, fixed_var_intercept = TRUE, fixed_var_trend = TRUE, fixed_var_variables = TRUE, date = 28, ... )
x |
a |
trend |
boolean indicating if the model should have a trend. |
var_intercept , var_slope
|
variance of the intercept (used if |
var_variables |
variance of the other variables: can be either a single value (same variance for all the variables) or a vector specifying each variance. |
fixed_var_intercept , fixed_var_trend , fixed_var_variables
|
logical indicating if the variance are fixed or estimated. |
date |
choose when we want to start the revision process after the start date. By default set to 28 periods. |
... |
other arguments used in |
Returns all coefficients of all variables and the residual