如何使用 MATLAB 估计定义我自己的协方差矩阵估计方法或给定协方差矩阵的回归系数?

问题描述

给定 y 因变量 nx1X 自变量的维度矩阵 nxp。我愿意使用给定的协方差矩阵或通过定义我自己的协方差矩阵的估计方法来估计 beta 系数、r 平方和其他线性回归参数。是否有任何允许这样做的 Matlab 函数

解决方法

好的,这就是我写的:

function [coefficients,intercept] = gvcoef(X,y,CovEst)
%
% Finds coefficients and intercept estimates
% Inputs:
% - y -> dependent variable vector nx1
% - X -> independent variables matrix nxp
% - cov -> given estimate of covariance matrix (cov(X) in OLS)
% - intercept -> if true intercept is included (default)

a = CovEst;

for i=1:size(X,2)
    covariance = cov(y,X(:,i));
    b(i,:)= covariance(1,2);
end

% Coefficients from the covariance matrix
coefficients = linsolve(a,b);

% Intercept
y_bar = mean(y);
x_bar = mean(X,1);
intercept = y_bar - x_bar * coefficients;

end