📄 hgui2.asv
字号:
xx=x;
[n,m]=size(x);
M=mean(x);
for i=1:m
l(i)=sqrt(sum((x(:,i)-M(i)).^2));
x1(:,i)=x(:,i)-M(i);
x(:,i)=x1(:,i)/l(i);
end
strall=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%样本离差阵、相关系数阵
for i=1:m
for j=1:m
R(i,j)=sum(x(:,i).*x(:,j));
S(i,j)=sum(x1(:,i).*x1(:,j));
end
end
s1='-----------------------多元逐步回归--------------------------';
s2='样本离差阵为:';
s3='相关系数阵为:';
str1={s1,'',s2,num2str(S),'',s3,num2str(R),''};
strall=[strall str1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%------逐步计算-------%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
in=0;
out=(1:1:m-1);
step=0;
Ft=0;
Fj=Fin+1;
V_in=0;
while (Fj>Fin)|(Ft<=Fout)
s4=['------------------------第',num2str(step+1),'步---------------------------'];
str2={s4};
%%%%%%%%%%%%%%%%%%%%%%-----------引入变量----------%%%%%%%%%%%%%%%%%%%%
s5='---------引入变量----------';
if out==0
s6='没有未引入的点';
str3={s5,'',s6};
strall=[strall str2 str3];
else
s6='未引入的点为:';
[V_out,vmax,dmax]=get_V(R,m,out);
s7='未入选的点的方差贡献为:';
k=out(dmax);
s8=['其中点',num2str(k),'的方差贡献最大'];
r2=R(m,m);
Fj=(vmax*(n-step-2))/(r2-vmax);
s9=['F(进)为:',num2str(Fj)];
%判断是否引入
if Fj>Fin
s10=['因为F(进)>Fin(引进门坎植),点',num2str(k),'引入'];
R=get_R(R,m,k);
s11='对R进行消失变换,R阵变换为:';
str3={s5,'',s6,num2str(out),'',s7,num2str(V_out),'',s8,'',s9,'',s10,'',s11,'',num2str(R),''};
strall=[strall str2 str3];
%在in中增加引入的点,在out中删除刚引入的点
if in==0
in=[k];
V_in=vmax;
out=newout(out,k);
else
in=[in k];
V_in=[V_in vmax];
out=newout(out,k);
end
else
s10=['因为F(进)<=Fin(引进门坎植),点',num2str(k),'不引入'];
str3={s5,'',s6,num2str(out),'',s7,num2str(V_out),'',s8,'',s9,'',s10,''};
strall=[strall str2 str3];
end
end
%%%%%%%%%%%%%%%%%%%%%------剔除变量------%%%%%%%%%%%%%%%%%%%%%%
s12='--------- 剔除变量----------';
if in==0
s13='没有已入选的点';
str4={s12,'',s13};
strall=[strall str4];
else
s13='已入选的点为:';
s14='已引入的点的方差贡献为:';
%取得已入选的点中方差贡献最小的点
[vmin,dmin]=min(V_in);
k=in(dmin);
s15=['其中点',num2str(k),'的方差贡献最小'];
%diag_R=diag(R);
%r1=diag_R(k);
r1=R(m,m);
Ft=(vmin*(n-step-1))/r1;
s16=['F(出)为:',num2str(Ft)];
%判断是否剔除
if Ft<=Fout
s17=['因为F(出)<=Fout(剔除门坎植),点',num2str(k),'剔除'];
%disp(['tichu',num2str(h)])
%对R进行消去变换
R=get_R(R,m,k);
s18='对R进行消失变换,R阵变换为:';
str4={s12,'',s13,num2str(in),'',s14,num2str(V_in),'',s15,'',s16,'',s17,'',s18,'',num2str(R),''};
strall=[strall str4];
% 在in中删除引入的点号和在out中增加刚剔除的点号
out=[out k];
[in,V_in]=newin(in,V_in,k);
else
s17=['因为F(出)>Fout(剔除门坎植),点',num2str(k),'不剔除'];
str4={s12,'',s13,num2str(in),'',s14,num2str(V_in),'',s15,'',s16,'',s17,''};
strall=[strall str4];
end
end
step=step+1;
end
str5={'已无变量可引入,也无变量可剔除'};
strall=[strall str5];
%在结果框中输出结果
%求回归系数
in=sort(in);
diag_S=diag(S);
B=(sqrt(S(m,m))./sqrt(diag_S(1:m-1))).*R(1:m-1,m);
B(out)=0;
b0=M(m)-sum(M(1:m-1)'.*B);
Y=xx(:,m);
X=xx(:,1:m-1);
Y1=X*B+b0;
B=[b0;B];
%误差分析
a=Y-Y1;
b=(Y-Y1)./Y*100;
N=[Y,Y1,a,b];
%syms k
%k(1:n)='';
%N=[' Y ','',' Y1 ','',' Y-Y1 ','',' ERROR%';num2str(N(:,1)),k(:),num2str(N(:,2)),k(:),num2str(N(:,3)),k(:),num2str(N(:,4))]
tit=[' Y ','',' Y1 ','',' Y-Y1 ','',' ERROR% '];
s19='回归方程的系数B为:';
s20='误差分析的结果为:';
str6={'',s19,num2str(B),'',s20,tit,num2str(N)};
strall=[strall str6];
set(handles2.outputdata_listbox,'string',strall);
%计算V
function [V_out,vmax,dmax]=get_V(R,m,out)
diag_R=diag(R);
V=(R(:,m).^2)./diag_R;
V_out=V(out);
[vmax,dmax]=max(V_out);
function R=get_R(R,m,k)
diag_R=diag(R);
r=diag_R(k);
R1=R;
for i=1:m
for j=1:m
R(i,j)=R1(i,j)-R1(i,k)*R1(k,j)/r;
end
end
R(k,:)=R1(k,:)/r;
R(:,k)=-R1(:,k)/r;
R(k,k)=1/r;
function arr=newout(arr,d)
arr1=0;
p=size(arr);
for i=1:p(2)
if arr(i)~=d
if arr1~=0
arr1=[arr1 arr(i)];
else
arr1=arr(i);
end
else
continue
end
end
arr=arr1;
function [in,V_in]=newin(in,V_in,d)
V_in1=0;
in1=0;
g=size(in);
for i=1:g(2)
if in(i)~=d
if in1~=0
in1=[in1 in(i)]
V_in1=[V_in1 V_in(i)];
else
in1=in(i);
V_in1=V_in(i);
end
else
continue
end
end
in=in1
V_in=V_in1% --- Executes during object creation, after setting all properties.function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc set(hObject,'BackgroundColor','white');else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit3 as text% str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes during object creation, after setting all properties.function help_listbox_CreateFcn(hObject, eventdata, handles)% hObject handle to help_listbox (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: listbox controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc set(hObject,'BackgroundColor','white');else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end% --- Executes on selection change in help_listbox.function help_listbox_Callback(hObject, eventdata, handles)% hObject handle to help_listbox (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: contents = get(hObject,'String') returns help_listbox contents as cell array% contents{get(hObject,'Value')} returns selected item from help_listbox% --- Executes on button press in readresult_button.function readresult_button_Callback(hObject, eventdata, handles2)% hObject handle to readresult_button (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)readresult_pushbutton(handles2);% --- Executes on button press in save_button.function save_button_Callback(hObject, eventdata, handles2)% hObject handle to save_button (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)save_pushbutton(handles2);% --- Executes on button press in exit_button.function pushbutton10_Callback(hObject, eventdata, handles)% hObject handle to exit_button (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function save_help_Callback(hObject, eventdata, handles)% hObject handle to save_help (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)save_help;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -