R中编码实现,作出偏相关以及中介作用的主要结果Bootstrap的置信区间。

视频:链接:http://pan.baidu.com/s/1pLoj6o7 密码:og0g

源代码如下:数据针对L26课ex1

偏相关:

library(RcmdrMisc)
library(car)
Dataset<-readSPSS(choose.files())
str(Dataset)
LAST<-array(dim = 1e4)
for (k in 1:1e4) {
x<-sample(nrow(Dataset),10,replace = T)
Data2<-Dataset[x,]
model1<-lm(Data2$timing~Data2$carbor)
summary(model1)
summary1<-summary(model1)
resid1<-summary1$residuals

model2<-lm(Data2$points~Data2$carbor)
summary(model2)
summary2<-summary(model2)
resid2<-summary2$residuals

LAST[k]<-cor(resid1,resid2)

}

hist(LAST,probability = T)
lines(density(LAST),lty=2)
quantile(LAST,c(.025,.975))

 

中介:

LAST2<-array(dim = 1e4)
for (k in 1:1e4) {
x<-sample(nrow(Dataset),10,replace = T)
Data2<-Dataset[x,]

modelA<-cor(Data2$timing,Data2$carbor)
modelB<-cor(Data2$points,Data2$timing)
LAST2[k]<-modelA*modelB

}

hist(LAST2,probability = T)
lines(density(LAST2),lty=2)
quantile(LAST2,c(.025,.975))