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

hayes model 5(조건부 직접효과)

by whitehandspsychology 2022. 11. 30.

model 5만 쓰는 연구는 본적이 없다. 

데이터 만들기는 생략 하겠다. 

각 변수 명을 쓰는 것이 아닌 데이터 d에서 몇번째 열이 해당 변수인지 쓰면 된다. 

boot5<-function(xxx,mmm,www,yyy,d,bootnum){
  ###estimate a*m
  boot5_1<-function(xxx,mmm,www,yyy,d){
    n<-sample(1:nrow(d),replace = T)
    nnk<-d[n,]
    nnk<-as.data.frame(nnk)
    k1<-lm(nnk[,mmm]~ nnk[,xxx], data=nnk)
    s1<-summary(k1)
    coem<-s1$coefficients
    eff<-as.data.frame(coem)
    eff<-eff[nrow(eff),1]
    k2<-lm(nnk[,yyy] ~ nnk[,xxx]+ nnk[,mmm] +nnk[,www] + nnk[,mmm]*nnk[,www], data = nnk)
    s2<-summary(k2)
    coem2<-s2$coefficients
    eff2<-as.data.frame(coem2)
    eff5<-eff2[nrow(eff2),1]
    eff3<-eff2[nrow(eff2)-2,1]
    meff<-eff*eff3
    dieff<-eff5
    efff<-c(meff, dieff)
    efff<-matrix(efff, ncol = 2)
    efff
  }
  k<-1
  l<-matrix(rep(NA,bootnum*2),ncol = 2)
  l<-as.data.frame(l)
  repeat{
    l[k,]<-boot5_1(xxx,mmm,www,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))
  kmkmkmkm<-list(c(mean(l[,1]),sd(l[,1])),ci1, c(mean(l[,2]),sd(l[,2])),ci2)
  names(kmkmkmkm)<-c("mediation_mean_BootSE", "mediation_CI","moderation_mean_BootSE", "moderation_CI" )
  kmkmkmkm
}

결과를 보자

boot5(1,2,3,4,d,000)

$mediation_mean_BootSE
[1] 1.1306273 0.1702732

$mediation_CI
     0.1%        1%        5%       10%       90%       95%       99%     99.9% 
0.6161618 0.7547732 0.8548217 0.9138608 1.3522673 1.4158794 1.5331950 1.6738370 

$moderation_mean_BootSE
[1] 0.44420070 0.08226611

$moderation_CI
     0.1%        1%        5%       10%       90%       95%       99%     99.9% 
0.1934341 0.2541125 0.3082653 0.3399593 0.5463655 0.5806235 0.6444123 0.7320048

 

 

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

[R]카이제곱분포 분포도  (0) 2023.05.02
연속형 변수 ~ 범주형 변수(외래관광실태조사)  (0) 2022.12.05
hayes model 4(매개모형)  (0) 2022.11.30
hayes model 3  (0) 2022.11.28
hayes model 2  (0) 2022.11.28

댓글