在繁杂的现场工作中找到片刻闲暇时间,加之最近又有读者问起,便继续更新之前的教程,希望能对大家有所帮助。本次主要分享的是线性回归以及logistic回归的内容~
1. 线性回归
按以往惯例,还是以官方教程提供的数据和代码为参考,采用的dataset与之前教程一致,就不再放链接了,直接上代码和结果
- SAS代码 (主要就是变量的重新赋值,为之后校正混杂因素等做好准备)
data analysis_data_1;
set analysis_data;
if ridstatr=2; *all mec exam data;
/*set don't know and refused (7,9) to missing*/
if dmdeduc>3 then dmdeduc=.;
/*define smokers*/
if smq020 eq 2 then smoker=1;
else if smq020 eq 1 and smq040 eq 3 then smoker=2;
else if smq020 eq 1 and smq040 in(1,2) then smoker=3;
/*for input to SAS PROC SURVEYREG - recode gender so that men is the reference group*/
if riagendr eq 1 then sex=2;
else if riagendr eq 2 then sex=1;
/*for input to SAS PROC SURVEYREG - recode race/ethnicity so that non-Hispanic white is the reference group*/
ethn=ridreth1;
if ridreth1 eq 3 then ethn=5;
else if ridreth1 eq 4 then ethn=2;
else if ridreth1 eq 2 then ethn=3;
else if ridreth1 eq 5 then ethn=4;
if 0 le bmxbmi lt 18.5 then bmicatf=1;
else if 18.5 le bmxbmi lt 25 then bmicatf=4;
else if 25 le bmxbmi lt 30 then bmicatf=2;
else if bmxbmi ge 30 then bmicatf=3;
if 0 le bmxbmi lt 18.5 then bmicat=1;
else if 18.5 le bmxbmi lt 25 then bmicat=2;
else if 25 le bmxbmi lt 30 then bmicat=3;
else if bmxbmi ge 30 then bmicat=4;
if (lbdhdl^=. and riagendr^=. and ridreth1^=. and smoker^=. and dmdeduc^=. and bmxbmi^=.)and wtmec4yr>0
and (ridageyr>=20) then eligible=1; *else eligible=2;
run;
- SAS代码 (简单线性回归以及多元线性回归)
/*简单线性回归*/
PROC SURVEYREG data=analysis_data_1 nomcar;
STRATA sdmvstra;
CLUSTER sdmvpsu;
WEIGHT wtmec4yr;
MODEL lbdhdl= bmxbmi/CLPARM vadjust=none;
DOMAIN eligible;
run;
/*多元线性回归*/
PROC SURVEYREG data=analysis_data_1 nomcar;
STRATA sdmvstra;
CLUSTER sdmvpsu;
WEIGHT wtmec4yr;
CLASS sex ethn smoker dmdeduc bmicatf;
DOMAIN eligible;
MODEL lbdhdl= sex ethn ridageyr smoker dmdeduc smoker bmicatf/CLPARM solution vadjust=none;
run;
-
结果展示
2.logistic回归
- SAS代码 (主要就是变量的重新赋值,为之后校正混杂因素等做好准备)
data analysis_data_1;
set analysis_data;
if ridstatr=2; *all mec examined data;
if ridageyr >= 20 then sel=1; *else sel=2;
*create an indicator of hypertension based on blood pressure and medication;
if (bpxsar >= 140 or bpxdar >= 90 or bpq050a = 1) then Hyper = 1;
else if (bpxsar ne . and bpxdar ne .) then Hyper = 0;
*create an indicator of high cholesterol based on total cholesterol and medication;
if (lbxtc>=240 or bpq100d = 1) then HiChol = 1;
else if (lbxtc ne . ) then HiChol = 0;
*create bmi groups;
if 0<=bmxbmi<25 then bmigrp=1;
else if 25<=bmxbmi<30 then bmigrp=2;
else if bmxbmi>=30 then bmigrp=3;
*create age groups;
if 20<=ridageyr<40 then age=1;
else if 40<=ridageyr<60 then age=2;
else if ridageyr>=60 then age=3;
*create log of fasting triglycerides;
logtrig=log(lbxtr);
run;
- SAS代码 (多元logistic回归)
PROC SURVEYLOGISTIC DATA = Analysis_Data_1 nomcar; /* can add "noprint" option to suppress printing if using ods statement*/
STRATA sdmvstra;
CLUSTER sdmvpsu;
WEIGHT wtsaf4yr;
DOMAIN sel;
CLASS age (PARAM=REF REF="2")
riagendr (PARAM=REF REF="2")
hichol (PARAM=REF REF="1")
bmigrp (PARAM=REF REF="2"); /*分别以2组和1组作为对照组*/
MODEL hyper (desc)=age riagendr hichol bmigrp logtrig/clparm vadjust=none;
run;
-
结果展示
一点总结
总体而言,演示程序并不复杂,完成前期的赋值,之后带入模型即可。但是程序中仍有可学习的地方,比如是否患高血压的赋值,满足收缩压>140mmHg或者舒张压>90mmHg或者服用高血压药物;通过lbdhdl^=.来排除缺失值,筛选最后需要的人群
4. 参考内容