Storing regression coefficients in a loop

6 ビュー (過去 30 日間)
Elin Jacobs
Elin Jacobs 2021 年 12 月 7 日
コメント済み: Elin Jacobs 2021 年 12 月 8 日
Hello,
I'm doing a simple linear regression on a 20x30x365 matrix (latitude, longitude, time). I want to store the slope and p - value for each lat/lon pair such that the end result are two 20x30 matrices. I'm attaching some dummy data and my attempted code below. Thank you.
X = size(prec,3);
i = 20;
j = 30;
slopes = zeros(i,j);
pvals = zeros(i,j);
for i = 1:i
for j= 1:j
mdl = fitlm(X,prec(i,j,:));
slope = table2array(mdl.Coefficients(2,1));
pval = table2array(mdl.Coefficients(2,4));
slopes = slope(i,j);
pvals = pval(i,j);
end
end

採用された回答

VBBV
VBBV 2021 年 12 月 7 日
編集済み: VBBV 2021 年 12 月 8 日
slopes(i,j) = slope;
pvals(i,j) = pval;
  2 件のコメント
VBBV
VBBV 2021 年 12 月 8 日
n = size(prec,3);
X = (1:1:n)';
I = 20; % change this variable with a differnt letter
J = 30; % same here
slopes = zeros(I,J);
pvals = zeros(I,J);
for i = 1:I
for j= 1:J
y = reshape(prec(i,j,:),[n,1]);
mdl = fitlm(X,y);
slope = table2array(mdl.Coefficients(2,1));
pval = table2array(mdl.Coefficients(2,4));
slopes(i,j) = slope;
pvals(i,j) = pval;
end
end
When the for loop runs, its using concurrent values of i and j. Change the symbols as above and run it
Elin Jacobs
Elin Jacobs 2021 年 12 月 8 日
My bad, this works great! thank you!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnalysis of Variance and Covariance についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by