%% step 1
hospital = readtable('hospital.xls','ReadRowNames',true);
hospital.smoke = nominal(hospital.smoke,{'No','Yes'});
%% step 2
model='sys ~ 1 + age + wgt + smoke';
mdl = fitlm(hospital, model)
plotResiduals(mdl)
outlier = mdl.Residuals.Raw > 9; %数据清洗条件
OL=find(outlier);
mdl = fitlm(hospital, model,'Exclude', OL);
mdl.ObservationInfo(OL,:)
mdl1 = step(mdl,'NSteps',10);
mdl1.Formula.disp
%% step 3
ages = [20;40;60];
smoker = {'Yes';'No';'Yes'};
predictnew = mdl1.feval(ages,smoker)
mdl1.Coefficients(:,1)
>> Case_52
mdl =
线性回归模型:
sys ~ 1 + age + wgt + smoke
估计系数:
Estimate SE tStat pValue
___________ ________ _________ __________
(Intercept) 116.28 3.6668 31.712 1.1478e-52
age 0.085808 0.066986 1.281 0.20329
wgt -0.00098391 0.018594 -0.052915 0.95791
smoke_Yes 9.8925 1.0353 9.5552 1.3434e-15
观测值数目: 100,误差自由度: 96
均方根误差: 4.78
R 方: 0.507,调整 R 方 0.492
F 统计量(常量模型): 33,p 值 = 9.91e-15
ans =
2×4 table
Weights Excluded Missing Subset
_______ ________ _______ ______
WXM-486 1 true false false
XBJ-540 1 true false false
1。正在删除 wgt, FStat = 0.25432, pValue = 0.61523
sys ~ 1 + age + smoke
predictnew =
126.9535
119.2712
132.0364
ans =
3×1 table
Estimate
________
(Intercept) 114.19
age 0.12707
smoke_Yes 10.224
开发工具:MATLAB 2022b
微信截屏工具 Alt+A