交互作用效应(p for Interaction)在SCI文章中可以算是一个必杀技,几乎在高分的SCI中必出现,因为把人群分为亚组后再进行统计可以增强文章结果的可靠性,不仅如此,交互作用还可以使用来进行数据挖掘。在既往文章中,我们已经介绍了怎么使用R语言可视化visreg包对交互作用进行可视化分析(见下图),

r语言 交互项怎么写 r语言交互作用怎么分析_logistic回归


r语言 交互项怎么写 r语言交互作用怎么分析_回归_02


在既往文章中,我们已经使用R语言和SPSS分别绘制了logistic回归交互效应的可视化分析,后台有粉丝希望做一期stata交互效应的可视化分析,现在我们来使用stata演示一下logistic回归交互项(交互作用)的可视化分析,继续使用我们的不孕症数据(公众号回复:不孕症,可以获得数据),这样有助于对结果进行比较。

我们先导入数据数据看一下

r语言 交互项怎么写 r语言交互作用怎么分析_logistic回归_03


数据有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"

r语言 交互项怎么写 r语言交互作用怎么分析_回归_04


我们进行的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

r语言 交互项怎么写 r语言交互作用怎么分析_回归_05


算出来的结果后,我们生成它的概率

predict fit

r语言 交互项怎么写 r语言交互作用怎么分析_logistic回归_06


生成概率后就可以做图了,主要是用到twoway这个函数做图,下面我们来由简单到复杂,慢慢深入应用这个函数作图,以便加深这个函数的了解。

我们先来做一个简单的点图,这个函数的主体是twoway,加上scatter表示绘制点图,如果加上line表示绘制线图,connected表示散点和线的连接图。下面代码中fit 和age是我们的变量,if spontaneous0 表示条件判断,意思是绘制spontaneous0 的fit 和age关系散点

twoway scatter fit age if spontaneous==0, sort

r语言 交互项怎么写 r语言交互作用怎么分析_数据挖掘_07


twoway scatter fit age if spontaneous0, sort和twoway (scatter fit age if spontaneous0, sort)的代码是一样的,等于用括号括起来。

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

r语言 交互项怎么写 r语言交互作用怎么分析_回归_08


下面我们把上面代码中的scatter换成connected,这样就是点线连接图

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

r语言 交互项怎么写 r语言交互作用怎么分析_logistic回归_09


我们已经做了spontaneous0的连接图,同理稍稍修改就可以做spontaneous1和spnotallow==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(-))

r语言 交互项怎么写 r语言交互作用怎么分析_r语言 交互项怎么写_10


这个图是散点连接图,好像有点丑,我们对它进行改动一下,在每个线图的后面加上一句(qfit fit age if spnotallow==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 )

r语言 交互项怎么写 r语言交互作用怎么分析_r语言 交互项怎么写_11


哎,这样好像好看一点了,好歹有个趋势,如果不想要散点

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

r语言 交互项怎么写 r语言交互作用怎么分析_logistic回归_12


如果不想要曲线可以改成直线

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

r语言 交互项怎么写 r语言交互作用怎么分析_数据挖掘_13


最后加上图例

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 "))

r语言 交互项怎么写 r语言交互作用怎么分析_数据挖掘_14


还可以修改图例位置

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))

r语言 交互项怎么写 r语言交互作用怎么分析_r语言 交互项怎么写_15


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