What effect Toothgrowth——Using T-test in R(Original)

#本页为上一部分内容(What effect ToothGrowth——Using SPSS Lmatrix)的补充,使用R语言重现上一部分的内容。使用数据和分析方法与上一部分相同。

attach(ToothGrowth)
table(supp,dose)
##     dose
## supp 0.5  1  2
##   OJ  10 10 10
##   VC  10 10 10
aggregate(len,by=list(supp,dose),FUN=mean)
##   Group.1 Group.2     x
## 1      OJ     0.5 13.23
## 2      VC     0.5  7.98
## 3      OJ     1.0 22.70
## 4      VC     1.0 16.77
## 5      OJ     2.0 26.06
## 6      VC     2.0 26.14
aggregate(len,by=list(supp,dose),FUN=sd)
##   Group.1 Group.2        x
## 1      OJ     0.5 4.459709
## 2      VC     0.5 2.746634
## 3      OJ     1.0 3.910953
## 4      VC     1.0 2.515309
## 5      OJ     2.0 2.655058
## 6      VC     2.0 4.797731
fit<-aov(len~supp*dose)
summary(fit)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## supp         1  205.4   205.4  12.317 0.000894 ***
## dose         1 2224.3  2224.3 133.415  < 2e-16 ***
## supp:dose    1   88.9    88.9   5.333 0.024631 *  
## Residuals   56  933.6    16.7                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(dose,supp,len,type="b",col=c("red","blue"),pch=c(16,18),main="Interaction between Dose and Supplement Type")

screen-shot-2016-12-26-at-10-04-42-pm
library(multcomp)
## Loading required package: mvtnorm
## Loading required package: survival
## Loading required package: splines
## Loading required package: TH.data

 

tuk<-glht(fit,linfct=mcp(supp="Tukey"))
plot(cld(tuk,level=.05),col="red") #用Tukey进行事后检验,画出箱型图,显示OJ与VC在排除dose影响时,对len的影响存在显著差异。
screen-shot-2016-12-26-at-10-04-52-pm

#以dose=0.5时VC与OJ的简单效应分析为例,在R中使用独立样本t检验来替代SPSS中Lmatrix的方式
newdata=subset(ToothGrowth,dose==0.5) #将数据中dose=1,dose=2的部分剔除,得到新的数据集
t.test(len~supp,data=newdata)#使用新数据集进行t检验
## 
##  Welch Two Sample t-test
## 
## data:  len by supp
## t = 3.1697, df = 14.969, p-value = 0.006359
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  1.719057 8.780943
## sample estimates:
## mean in group OJ mean in group VC 
##            13.23             7.98
##其他简单效应也可使用该方法获得,在此不做演示。
##所得结果与SPSS中有所不同可能是由于R中默认方差不相等。

 

What effectToothgrowth?—Using SPSS Lmatrix(Original)

学号:16210730029         课程:高级心理和行为科学统计     学期:2016年秋
视频地址:https://pan.baidu.com/s/1jIluEME
数据下载地址:https://pan.baidu.com/s/1hsE4YCO
数据来源:R语言基础数据包ToothGrowth。
数据分析:
自变量——喂食方式supp(橙汁OJ,维生素VC),抗败血酸含量dose(0.5,1,2);因变量——老鼠牙齿生长长度len
两因素方差分析及事后多重检验;使用Lmatrix语句进行简单效应分析。

 

OLS Regression: What effects health?(Original)

library(foreign)
Dataset <- read.spss("/Users/bean/Desktop/dataset.sav", 
  use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)
## Warning in read.spss("/Users/bean/Desktop/dataset.sav", use.value.labels =
## TRUE, : /Users/bean/Desktop/dataset.sav: Unrecognized record type 7,
## subtype 18 encountered in system file
colnames(Dataset) <- tolower(colnames(Dataset))
library(relimp, pos=16)
newdata=na.omit(Dataset) ##删除数据缺失值
summary(newdata)
##       education               workinglife  obstructivepressure
##  highschool:  2   <1 year           : 80   Min.   :1.000      
##  bachlor   :125   1-5years          :119   1st Qu.:1.909      
##  <graduate :155   6-9years          : 63   Median :2.545      
##                   more than 10 years: 20   Mean   :2.585      
##                                            3rd Qu.:3.182      
##                                            Max.   :5.000      
##                                                               
##  incentivepressure    workload     organizationlimitation stressreaction 
##  Min.   :2.000     Min.   :1.200   Min.   :1.000          Min.   :1.222  
##  1st Qu.:4.143     1st Qu.:2.200   1st Qu.:1.333          1st Qu.:2.111  
##  Median :4.857     Median :2.800   Median :1.833          Median :2.556  
##  Mean   :4.785     Mean   :2.837   Mean   :1.989          Mean   :2.597  
##  3rd Qu.:5.429     3rd Qu.:3.400   3rd Qu.:2.583          3rd Qu.:3.000  
##  Max.   :7.000     Max.   :5.000   Max.   :4.750          Max.   :4.333  
##                                                                          
##   performance     satisfaction   resignintention      ocb       
##  Min.   :1.143   Min.   :1.571   Min.   :1.000   Min.   :1.125  
##  1st Qu.:5.143   1st Qu.:3.286   1st Qu.:1.000   1st Qu.:3.125  
##  Median :6.000   Median :4.000   Median :2.000   Median :3.750  
##  Mean   :5.702   Mean   :3.879   Mean   :2.124   Mean   :3.699  
##  3rd Qu.:6.393   3rd Qu.:4.429   3rd Qu.:3.000   3rd Qu.:4.219  
##  Max.   :7.000   Max.   :6.000   Max.   :5.000   Max.   :5.000  
##                                                                 
##    detachment    taskorientation    security     superiorsupport
##  Min.   :1.000   Min.   :1.000   Min.   :1.500   Min.   :2.000  
##  1st Qu.:1.750   1st Qu.:3.286   1st Qu.:3.271   1st Qu.:4.000  
##  Median :2.250   Median :3.714   Median :3.750   Median :4.667  
##  Mean   :2.282   Mean   :3.629   Mean   :3.706   Mean   :4.693  
##  3rd Qu.:2.750   3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:5.292  
##  Max.   :5.000   Max.   :5.000   Max.   :5.000   Max.   :7.000  
##                                                                 
##     serious      workingcontrol   requirement        health     
##  Min.   :2.778   Min.   :1.857   Min.   :1.000   Min.   :1.000  
##  1st Qu.:3.444   1st Qu.:3.839   1st Qu.:3.125   1st Qu.:2.400  
##  Median :3.889   Median :4.286   Median :3.750   Median :3.200  
##  Mean   :3.847   Mean   :4.264   Mean   :3.702   Mean   :3.328  
##  3rd Qu.:4.222   3rd Qu.:4.714   3rd Qu.:4.250   3rd Qu.:4.200  
##  Max.   :5.000   Max.   :6.143   Max.   :6.500   Max.   :6.800  
##                                                                 
##      stress     
##  Min.   :1.722  
##  1st Qu.:3.000  
##  Median :3.389  
##  Mean   :3.441  
##  3rd Qu.:3.889  
##  Max.   :5.111  
## 
library(leaps)
leaps<-regsubsets(health~stress+superiorsupport+workingcontrol+requirement,data=newdata,nbest=2)
plot(leaps,scale = "adjr2")##全子集回归比较,选择第一行模型
全子集回归
fit<-lm(health~stress+superiorsupport+workingcontrol+requirement,data=newdata) summary(fit) ##建立回归模型
## 
## Call:
## lm(formula = health ~ stress + superiorsupport + workingcontrol + 
##     requirement, data = newdata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.0770 -0.6074 -0.0202  0.6161  3.4983 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      2.10787    0.52528   4.013 7.73e-05 ***
## stress           0.26140    0.11218   2.330   0.0205 *  
## superiorsupport -0.14649    0.07243  -2.022   0.0441 *  
## workingcontrol  -0.21636    0.09108  -2.375   0.0182 *  
## requirement      0.52165    0.07278   7.167 6.96e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9977 on 277 degrees of freedom
## Multiple R-squared:  0.372,  Adjusted R-squared:  0.363 
## F-statistic: 41.03 on 4 and 277 DF,  p-value: < 2.2e-16
library(car)
vif(fit)#multicollinearity多重共线性检查
##          stress superiorsupport  workingcontrol     requirement 
##        1.406168        1.558698        1.624382        1.506382
sqrt(vif(fit))>2 
##          stress superiorsupport  workingcontrol     requirement 
##           FALSE           FALSE           FALSE           FALSE
##均无该问题
plot(fit) ## 回归诊断
screen-shot-2016-12-23-at-11-08-11-pm ##残差值与拟合值无系统关联,满足线性screen-shot-2016-12-23-at-11-08-20-pm##残差值为一个均值为0的正态分布,满足正态性 screen-shot-2016-12-23-at-11-08-26-pm##水平线周围的点随机分布,满足同方差性 screen-shot-2016-12-23-at-11-08-31-pm##349和127为两个强影响点,但由于无特殊原因,不删除
APA格式报告: 
A multiple regression analysis was conducted to predict health from stress, superior support, working control and requirement. The results of this analysis indicated that stress, superior support, working control and requirement accounted for a significant amount of health variability. R2 = .37, adjusted R2=.36, (4, 277) = 41.03, <. 01, indicating that higher stress, higher working requirement, lower superior support and lower working control tended to lead to more health problems.