加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

R做回归中的调节效应interaction effect

(2014-12-23 20:18:07)
标签:

股票

调节效应

回归

交互项

分类: 02研究方法
之前的帖子 Previous Link,写了STATA做交互效用图的程序,这里是R的实现办法。code 。在下面的例子中,x1 x2(调节变量) 均是 连续型变量。
以下是步骤:3 F  w: u2 u1 [+ v7 l+ _9 Y
1. 假设X -> Y , 调节变量(交互作用相同)是Z
2. 找出Z的平均数(Mean),标准差(SD)和中位数(Med)
3. 在数据中用做指标 (index)把数据排序,从Z最小到Z最大;2 \0 j# d- e0 p) _- D

方法一:
1. 把数据用来分,用Med所有数据分成两组,3 Y* @# r# E# Z# @
  一组是Z;
  一组是Z>Med# I7 T- u; Z- U3 n7 @, O, I
2. 在每一组做一个XY的回归分析,找出截距斜率,画出两条直线。6 W3 i+ o) y9 t; m5 p- Q( x' a
, Y4 \8 K6 N, X) O  G! x" C7 E9 |
方法二:  h2 x: k) t4 M8 V, k" m% }% |
1. 把数据用来分,用Mean所有数据分成两组,
  一组是Z;4 E5 U/ n/ A. i  z2 ^9 I
  一组是Z>Mean+SD
  中间的数据不要;
2. 在每一组做一个XY的回归分析,找出截距斜率,画出两条直线。

Statistics and Probability Dictionary

Select a term from the dropdown text box. The online statistics glossary will display a definition, plus links to other related web pages.

Select term:       

z Score

z-score (aka, a standard score) indicates how many standard deviations an element is from the mean. A z-score can be calculated from the following formula.

z = (X - μ) / σ

where z is the z-score, X is the value of the element, μ is the population mean, and σ is the standard deviation.

Here is how to interpret z-scores.

  • A z-score less than 0 represents an element less than the mean.
  • A z-score greater than 0 represents an element greater than the mean.
  • A z-score equal to 0 represents an element equal to the mean.
  • A z-score equal to 1 represents an element that is 1 standard deviation greater than the mean; a z-score equal to 2, 2 standard deviations greater than the mean; etc.
  • A z-score equal to -1 represents an element that is 1 standard deviation less than the mean; a z-score equal to -2, 2 standard deviations less than the mean; etc.
  • If the number of elements in the set is large, about 68% of the elements have a z-score between -1 and 1; about 95% have a z-score between -2 and 2; and about 99% have a z-score between -3 and 3.

Here is another way to think about z-scores. A z-score is the normal random variable of a standard normal distribution .


How can one plot continuous by continuous interactions in ggplot2?   link

Let's say I have data:


x1 <- rnorm(100,2,10)
x2 <- rnorm(100,2,10)
y <- x1+x2+x1*x2+rnorm(100,1,2)
dat <- data.frame(y=y,x1=x1,x2=x2)
res <- lm(y~x1*x2,data=dat)
summary(res)

I want to plot the continuous by continuous interaction such that x1 is on the X axis and x2 is represented by 3 lines, one which represents x2 at a Z-score of 0, one at Z-score of +1, and another at a Z-score of -1, with each line a separate color and labelled. How can I do this using ggplot2?

For example, it might look something like this (though of course with different colored lines rather than different line types): 

http://s1/mw690/001Rt2X3zy6OC3KlyTe40&690effect" TITLE="R做回归中的调节效应interaction effect" />
Here's my version with your simulated data set:

x1 <- rnorm(1000,20,10)

x2 <- rnorm(1000,1,10)

y <- x1+x2+x1*x2+rnorm(100,1,2)

dat <- data.frame(y=y,x1=x1,x2=x2)

res <- lm(y~x1*x2,data=dat)

z1 <- z2 <- seq(-1,1)

newdf <- expand.grid(x1=z1,x2=z2)


library(ggplot2)

p <- ggplot(data=transform(newdf, yp=predict(res, newdf)), 

            aes(y=yp, x=x1, color=factor(x2))) + stat_smooth(method=lm)

p + scale_colour_discrete(name="x2") + 

  labs(x="x1", y="mean of resp") + 

  scale_x_continuous(breaks=seq(-1,1)) + theme_bw()



dat$y0 <- res$coefficients[[1]] + res$coefficients[[2]]*dat$x1 + res$coefficients[[3]]*0 + res$coefficients[[4]]*dat$x1*0

dat$y1m <- res$coefficients[[1]] + res$coefficients[[2]]*dat$x1 + res$coefficients[[3]]*-1 + res$coefficients[[4]]*dat$x1*-1

dat$y1p <- res$coefficients[[1]] + res$coefficients[[2]]*dat$x1 + res$coefficients[[3]]*1 + res$coefficients[[4]]*dat$x1*1



plot(dat$x1, dat$y0, type="l", xlab="x1", ylab="Estimates")

lines(dat$x1, dat$y1m, col="red")

 

lines(dat$x1, dat$y1p, col="blue")


http://s2/mw690/001Rt2X3zy6OC3WLRcJ31&690effect" TITLE="R做回归中的调节效应interaction effect" />

http://s11/mw690/001Rt2X3zy6OC3WXDYu2a&690effect" TITLE="R做回归中的调节效应interaction effect" />

http://s16/mw690/001Rt2X3zy6OC3WZpxl4f&690effect" TITLE="R做回归中的调节效应interaction effect" />

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有