# How to find the row wise linear regression coefficients?

3 ビュー (過去 30 日間)
Abhishek Chakraborty 2021 年 10 月 29 日
コメント済み: Abhishek Chakraborty 2021 年 10 月 30 日
I have 3 matrices Y, x1, and x2 each having 3420 rows and 29 columns. I want the row wise linear regression coefficients of Y on x1, x2, and the interaction between x1 and x2 for their corresponding rows such that the regression coefficient term for each of them is of 3420 rows and 1 column.
I tried the following code:
y=rand([3420,28]);
x1=rand([3420,28]);
x2=rand([3420,28]);
n = 3420;
b = zeros(n,4);
X=zeros(n,4);
for i=1:n
X(i,4)=[ones([28,1]) x1(i,:)' x2(i,:)' (x1(i,:).*x2(i,:))'];
b(i,4)=(regress(y(i,:)',X(i,4)))';
end
But it is not giving the desired result.
How to get the linear regression coefficients of Y on x1, x2, and interaction between x1, x2 as a 3420 rows and 1 column array?

サインインしてコメントする。

### 採用された回答

Ive J 2021 年 10 月 30 日
nsample = 29;
nvar = 3420;
% sample data
y = rand(nsample, nvar);
x1 = rand(nsample, nvar);
x2 = rand(nsample, nvar);
xInt = x1.*x2;
intercept = ones(nsample, 1);
beta = zeros(nvar, 4);
for i = 1:nvar
desMat = [intercept, x1(:, i), x2(:, i), xInt(:, i)];
beta(i, :) = regress(y(:, i), desMat);
end
size(beta)
ans = 1×2
3420 4
Each row of beta contains coefficients for each variables in nvar.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Abhishek Chakraborty 2021 年 10 月 30 日
Thanks a lot Ive J.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeTesting Frameworks についてさらに検索

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by