- Update the data and independent variables: X = data(3:42, 2:end);
- Adjust the function definition of the Project_func function. You can change by using the linear combination of independent variables and modifying the model equation.
- Modify the bootstrap loop to include all independent variables. This is an example code for the changes in your previous code.
Boost Trap for multivariant model
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to do boost trap for 2 independent variables and I am not sure how to do it. I have already done the boost trap for 1 independent variable by boost trapping the residuals. The code for 1 independent variable is given below. Data sheet "Data.xlsx" is also attached.
data =readmatrix('Data.xlsx');
x1=data(3:42,2);
x2=data(3:42,3);
Cobs=data(3:42,4);
xm=[x1 x2];
%% Initial parameter guesses
C1=1.3585;
C2=0.1749;
beta0(1)=C1; %initial guess yo
beta0(2)=C2; %initial guess kr
n=length(x1);
p=length(beta0);
%% bootstrap CB for beta
fnameFOR = @Project_func;
% use nlinfitcheck to ignore NaN
nlinfitcheck = statset('nlinfit');
nlinfitcheck.FunValCheck='off';
nboot=100;
betab(1,:)=beta;%put beta from original data as first row
ypredb(1,:)=Cpred;%put Cpred from original data as first row
%mm=1;%use x1, y1; x2, y2;... bootstrapping the data
mm=2;%use x1, Ypred1+e1; x2, Ypred2+e2;... bootstrapping the residuals
for j=2:nboot
r=round(1 + (n-1).*rand(n,1));%index of random integers from 1 to n
for i=1:n
if mm==1
tt(i)=t(r(i));%tt(i) is the time for each bootstrapped datum
yboot(i)=Cobs(r(i));%yboot(i) is the y value for each bootstrapped datum
end
if mm==2
tt=t;
yboot(i)=Cpred(i)+resids(r(i));
if i==n
yboot=yboot'; %make yboot a column
end
end
end
[betab(j,:),rr(j,:),J2,COVB2,mse2]= nlinfit(tt,yboot,fname,beta0,nlinfitcheck);%betab are the parameters from the bootstrapping
ypredb(j,:)=fname(betab(j,:),t);%should change "t" to tpred(1000,1) using linspace
clear yboot
end
r2=rr(1,:)';%save the residuals from first bootstrapping
%r2=resids;
for j=2:nboot
r2=[r2; rr(j,:)']; %build a matrix "r2" where each column holds residuals for one bootstrap
end
bsort=sort(betab,1); ysort=sort(ypredb,1); %"1" means sorts along columns
L=round(0.025*nboot); %L is lower 2.5% cutoff
if L==0; L=1; end %if L = 0, switch to L = 1 to avoid MATLAB error
U=round(0.975*nboot); %U is upper 2.5% cutoff
cib(1,1)=bsort(L,1); cib(1,2)=bsort(U,1);%bootstrap 95% CI for first beta
cib(2,1)=bsort(L,2); cib(2,2)=bsort(U,2);%bootstrap 95% CI for second beta
for i=1:n
ybci(i,1)=ysort(L,i); ybci(i,2)=ysort(U,i);%ybci is a n-by-2 matrix with bootstrap CI for y at each time
end
%% compute bootstrap prediction bands
D=rmse*tinv(.975,n-p);
CIwb(:,1)=ybci(:,1)-Cpred; CIwb(:,2)=Cpred-ybci(:,2); %upper (column 1) and lower (column 2) bootstrap CIwidths
PIwb(:,1)=sqrt(CIwb(:,1).^2+D^2); PIwb(:,2)=sqrt(CIwb(:,2).^2+D^2);%upper and lower widths of PI
PIb(:,1)=Cpred+PIwb(:,1); PIb(:,2)=Cpred-PIwb(:,2); %PI values
%% Functions
function y=Project_func(beta0,x1,x2)
c2s=@(x)-2.40874-39.748*(1+x).^-2.856;
y=100./(1+exp(-beta0(1).*c2s(x1)+(beta0(2).*c2s(x1).*log10(100.*x2))));
end
댓글 수: 0
답변 (1개)
Shivansh
2023년 9월 12일
Hi Faizan,
I understand that you have a working code for one independent variable. You want to know the way to generalise the above algorithm for the multivariate case.
To generalize the bootstrap approach for a multivariate model with multiple independent variables, you would need to make a few modifications to the code you provided. Here's a general outline of the steps involved:
for j = 2:nboot
r = round(1 + (n - 1) .* rand(n, 1)); % Index of random integers from 1 to n
for i = 1:n
if mm == 1
tt(i) = t(r(i)); % tt(i) is the time for each bootstrapped datum
yboot(i) = Cobs(r(i)); % yboot(i) is the y value for each bootstrapped datum
end
if mm == 2
tt = t;
yboot(i) = Cpred(i) + resids(r(i));
if i == n
yboot = yboot'; % Make yboot a column
end
end
end
[betab(j, :), rr(j, :), J2, COVB2, mse2] = nlinfit(tt, yboot, @(beta) Project_func(beta, X), beta0, nlinfitcheck);
ypredb(j, :) = Project_func(betab(j, :), X);
clear yboot
end
These modifications will enable your code to generalise for multivariate cases. Make sure to adjust the code with respect to the changes.
Hope this helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Events에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!