stata行logistic回归交互项(交互作用)的可视化分析(1)

交互作用效应(p for Interaction)在SCI文章中可以算是一个必杀技,几乎在高分的SCI中必出现,因为把人群分为亚组后再进行统计可以增强文章结果的可靠性,不仅如此,交互作用还可以使用来进行数据挖掘。在既往文章中,我们已经介绍了怎么使用R语言可视化visreg包对交互作用进行可视化分析(见下图),
在这里插入图片描述
在这里插入图片描述
在既往文章中,我们已经使用R语言和SPSS分别绘制了logistic回归交互效应的可视化分析,后台有粉丝希望做一期stata交互效应的可视化分析,现在我们来使用stata演示一下logistic回归交互项(交互作用)的可视化分析,继续使用我们的不孕症数据(公众号回复:不孕症,可以获得数据),这样有助于对结果进行比较。
我们先导入数据数据看一下
在这里插入图片描述
数据有8个指标,最后两个是PSM匹配结果,我们不用理他,其余六个为:
Education:教育程度,age:年龄,parity产次,induced:人流次数,case:是否不孕,这是结局指标,spontaneous:自然流产次数。
先把Education这个指标转成数字

g edu=2
replace edu=0 if education=="0-5yrs"
replace edu=1 if education=="6-11yrs"

在这里插入图片描述
我们进行的logistic回归可视化,肯定要先建立logistic回归模型,假设我们想知道age和spontaneous有无交互,模型可以界面操作也可以代码,我就直接代码了,这里要说明一下,在stata中#号代表交互的意思(和R不一样,R是*号),age前面加个c.代表这是连续变量,spontaneous前面加个i.代表这是分类变量

logistic case age i.edu parity i.induced i.spontaneous c.age#i.spontaneous 

在这里插入图片描述
算出来的结果后,我们生成它的概率

predict fit

在这里插入图片描述
生成概率后就可以做图了,主要是用到twoway这个函数做图,下面我们来由简单到复杂,慢慢深入应用这个函数作图,以便加深这个函数的了解。
我们先来做一个简单的点图,这个函数的主体是twoway,加上scatter表示绘制点图,如果加上line表示绘制线图,connected表示散点和线的连接图。下面代码中fit 和age是我们的变量,if spontaneous0 表示条件判断,意思是绘制spontaneous0 的fit 和age关系散点

twoway scatter fit age if spontaneous==0, sort

在这里插入图片描述
twoway scatter fit age if spontaneous0, sort和twoway (scatter fit age if spontaneous0, sort)的代码是一样的,等于用括号括起来。

twoway (scatter fit age if spontaneous==0, sort)

在这里插入图片描述
下面我们把上面代码中的scatter换成connected,这样就是点线连接图

twoway (connected fit age if spontaneous==0, sort)

在这里插入图片描述
我们已经做了spontaneous0的连接图,同理稍稍修改就可以做spontaneous1和spontaneous==2的连接图,3个线条同时作图

twoway (connected fit age if spontaneous==0, sort) (connected fit age if spontaneous==1, sort lp(-)) (connected fit age if spontaneous==2, sort lp(-))

在这里插入图片描述
这个图是散点连接图,好像有点丑,我们对它进行改动一下,在每个线图的后面加上一句(qfit fit age if spontaneous==n),就可以根据散点拟合线条

twoway (scatter fit age if spontaneous==0, sort) (qfit fit age if spontaneous==0 )(scatter fit age if spontaneous==1, sort lp(-))(qfit fit age if spontaneous==1 ) (scatter fit age if spontaneous==2, sort lp(-)) (qfit fit age if spontaneous==2 )

在这里插入图片描述
哎,这样好像好看一点了,好歹有个趋势,如果不想要散点

twoway (qfit fit age if spontaneous==0 )(qfit fit age if spontaneous==1 ) (qfit fit age if spontaneous==2 )

在这里插入图片描述
如果不想要曲线可以改成直线

twoway (lfit fit age if spontaneous==0 )(lfit fit age if spontaneous==1 ) (lfit fit age if spontaneous==2 )

在这里插入图片描述
最后加上图例

twoway (lfit fit age if spontaneous==0 )(lfit fit age if spontaneous==1 ) (lfit fit age if spontaneous==2 ),legend(lab(1 "spontaneous==0 ") lab(2 "spontaneous==1 ") lab(3 "spontaneous==3 "))

在这里插入图片描述
还可以修改图例位置

twoway (lfit fit age if spontaneous==0 )(lfit fit age if spontaneous==1 ) (lfit fit age if spontaneous==2 ),legend(lab(1 "spontaneous==0 ") lab(2 "spontaneous==1 ") lab(3 "spontaneous==3 ") ring(0) pos(1))

在这里插入图片描述
这样基本就做好了,还可以加上标题和修改X轴和Y轴名字等等,我就不一一演示了。Twoway这个函数还有很多强大的功能,我们将在以后的文章中一一演示。另外COX回归的交互可视化做法也差不多,有兴趣的可以自己试试。
在这里插入图片描述

  • 10
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天桥下的卖艺者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值