본문 바로가기
  • WHP's 이야기
기타 잡기장

hayes model 3

by whitehandspsychology 2022. 11. 28.

가장 인기있지만 3박자가 맞아야 하는 조절된 조절이다. 첫박은 이론적으로 근거가 막강하게 가설을 잡아야 하고 둘째로 데이터 수집도 말끔하게 해야 하며 마지막으로 이상한 표본도 없거나 effect가 표본수 대비 충분해 줘야 하는 것 같다.

데이터 부터 만들어 보자.

x<-rnorm(100)
w1<-rnorm(100)+ x^2
w2<-rnorm(100)+ abs(x)
y<-rnorm(100, 0,1)  + w1*w2*x
co1<-rnorm(100)

d<-data.frame(x,w1,w2,y,co1)

 

boot3<-function(xxx,mmm, mmm2,yyy,d,bootnum){
  ###estimate a*m
  boot3_1<-function(xxx,mmm,mmm2, yyy,d){
    n<-sample(1:nrow(d),nrow(d),replace = T)
    nnk<-d[n,]
    nnk<-as.data.frame(nnk)
    k2<-lm(nnk[,yyy]~ nnk[,xxx]+nnk[,mmm] + nnk[,mmm2]+ nnk[,xxx]*nnk[,mmm] + nnk[,xxx]*nnk[,mmm2] + nnk[,xxx]*nnk[,mmm]*nnk[,mmm2], data=nnk)
    s2<-summary(k2)
    coem<-s2$coefficients
    eff<-as.data.frame(coem)
    eff1<-eff[nrow(eff)-2,1]
    eff2<-eff[nrow(eff)-1,1]
    eff3<-eff[nrow(eff),1]
    efff<-c(eff1, eff2, eff3)
    efff<-matrix(efff, ncol = 3)
    efff
  }
  k<-1
  l<-matrix(rep(NA,bootnum*3),ncol = 3)
  l<-as.data.frame(l)
  repeat{
    l[k,]<-boot3_1(xxx,mmm, mmm2,yyy,d)
    k<-k+1
    if(k>=bootnum+1) break
  }
  estimates<-list(l)
  ci1<-quantile(l[,1],probs = c(.001,0.01,0.05,0.10,0.90,0.95,0.99,.999))
  ci2<-quantile(l[,2],probs = c(.001,0.01,0.05,0.10,0.90,0.95,0.99,.999))
  ci3<-quantile(l[,3],probs = c(.001,0.01,0.05,0.10,0.90,0.95,0.99,.999))
  kmkmkmkm<-list(c(mean(l[,1]),sd(l[,1])),ci1, c(mean(l[,2]), sd(l[,2])),ci2,
                 c(mean(l[,3]),sd(l[,3])),ci3)
  names(kmkmkmkm)<-c("moderation_mean_BootSE_x*w1", "moderation_CI_x*w1","moderation_mean_BootSE_x*w2", "moderation_CI_x*w2", "moderation_mean_BootSE_x*w1*w2", "moderation_CI_x*w1*w2")
  kmkmkmkm
}

 

막 만든 데이터라 유의하지 않다.

boot3(1,2,3,4,d,1000)

$`moderation_mean_BootSE_x*w1`
[1] 0.9347755 0.1685584

$`moderation_CI_x*w1`
     0.1%        1%        5%       10%       90%       95%       99%     99.9% 
0.4957663 0.6142070 0.6843268 0.7289439 1.1602119 1.2217586 1.4037690 1.5743151 

$`moderation_mean_BootSE_x*w2`
[1] 0.007263357 0.059032701

$`moderation_CI_x*w2`
       0.1%          1%          5%         10%         90%         95%         99%       99.9% 
-0.24961595 -0.15938998 -0.09916184 -0.07266851  0.07595744  0.08879090  0.13960394  0.17953389 

$`moderation_mean_BootSE_x*w1*w2`
[1] -0.01454421  0.07065602

$`moderation_CI_x*w1*w2`
       0.1%          1%          5%         10%         90%         95%         99%       99.9% 
-0.31850929 -0.23549835 -0.15771775 -0.10560361  0.05762863  0.07463888  0.10941380  0.15503864

 

'기타 잡기장' 카테고리의 다른 글

연속형 변수 ~ 범주형 변수(외래관광실태조사)  (0) 2022.12.05
hayes model 5(조건부 직접효과)  (0) 2022.11.30
hayes model 4(매개모형)  (0) 2022.11.30
hayes model 2  (0) 2022.11.28
Hayes Model 1  (0) 2022.11.27

댓글