Add constants and time trends to multivariate regression Matlab

5 views (last 30 days)
Hi Matlab,
I'm want to add a constant for each indepedent variable and a time trend for each independent variable in a multivariate regression.
Any suggestions how to do this?
Follwing this link https://se.mathworks.com/help/stats/mvregress.html it says 'to add a constant to a model, should use a col of ones.' Clearly,
we cannot add more than one col of ones.
Code follows below:
A = rand(200,1);
B = rand(200,1);
vX = [A B];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%define independent and dependent variables
K1 = lagmatrix(diff(vX),1)
K3 = lagmatrix(vX,1)
K4 = diff(vX)
%make same size
K1(any(isnan(K1), 2), :) = [];
K3(any(isnan(K3), 2), :) = [];
K4(any(isnan(K4), 2), :) = [];
K3(1,:) = [];
K4(1,:) = [];
%one trend for each series
%one constant for each series
%constant
con = [ones(198,1) ]
%trend
trend = [(1:198)' ]
[beta,Sigma,E,CovB] = mvregress([con K3 K1 trend], K4, 'vartype', 'fisher');

Accepted Answer

Dr.GADDALA JAYA RAJU
Dr.GADDALA JAYA RAJU on 15 Mar 2024
To add a constant term and a time trend for each independent variable in a multivariate regression, you can simply create additional independent variables that represent these terms. Here's how you can do it:
  1. Constant Term: Add a column of ones to your independent variable matrix. This column represents the constant term in the regression equation.
  2. Time Trend: Create a column representing the time trend for each independent variable. This could be a sequence of integers representing time points.
Let's say you have n independent variables 1,2,,X1,X2,,Xn and m observations. Your design matrix X will then have m rows and +2n+2 columns: one column for the constant term, one column for the time trend for each independent variable, and n columns for the original independent variables.
Here's a Python example using NumPy to illustrate how you can create such a design matrix:
import numpy as np
# Example independent variables
X1 = np.random.rand(100) # Independent variable 1
X2 = np.random.rand(100) # Independent variable 2
# Assume X1 and X2 are time series data
# Create constant term
constant_term = np.ones_like(X1)
# Create time trend for each independent variable
time_trend = np.arange(len(X1))
# Stack all independent variables together
X = np.column_stack((constant_term, time_trend, X1, X2))
# Print the shape of the design matrix
print("Shape of design matrix X:", X.shape)
In this example, X1 and X2 represent two independent variables (could be more in a real scenario). We create a constant term (constant_term) and a time trend (time_trend) for each independent variable. Then, we stack these together along with the original independent variables to form the design matrix X.
You can use this design matrix X along with your dependent variable y to perform multivariate regression using any regression method or library of your choice, such as statsmodels or scikit-learn in Python. These libraries typically provide functions or classes for fitting regression models with multiple independent variables.
  1 Comment
DGM
DGM on 26 Mar 2024
Why did you post a python example to a question about MATLAB mvregress()?
Why was it accepted?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!