64 kids could be used as further analysis No.5 is not a good item
library(reshape);library(ggplot2);library(dplyr)
## Warning: package 'ggplot2' was built under R version 3.2.4
##
## Attaching package: 'dplyr'
##
## The following object is masked from 'package:reshape':
##
## rename
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(corrplot);library(Hmisc);library(candisc);library(lm.beta)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
##
## Attaching package: 'Hmisc'
##
## The following objects are masked from 'package:dplyr':
##
## combine, src, summarize
##
## The following objects are masked from 'package:base':
##
## format.pval, round.POSIXt, trunc.POSIXt, units
##
## Loading required package: car
## Loading required package: heplots
##
## Attaching package: 'candisc'
##
## The following object is masked from 'package:stats':
##
## cancor
dta <- read.csv("data/infantexp2.csv",h=T)
head(dta);tail(dta);dim(dta)
dta <- dta[1:100,1:29]
dta <- dta[,-10] #delete No.5
newdata <- na.omit(dta);attach(newdata)
newdata$affective <- No1+No2+No3+No4+No6
newdata$cognitive <- No7+No8+No9+No10+No11
newdata$behavioral <- No12+No13+No14+No15+No16+No17+
No18+No19+No20+No21+No22+No23+No24
newdata <- newdata[,-(6:28)]
newdata$Age <- newdata$Age/365
newdata$ToM <- newdata$ToM_Neutral+newdata$ToM_Emapthy
show the data
## Sex Class Age ToM_Neutral ToM_Emapthy affective cognitive
## 1 1 Big 6.268493 5 9 12 14
## 2 2 Big 6.136986 7 4 12 15
## 3 2 Big 6.095890 6 0 13 15
## 5 1 Big 6.084932 5 3 13 15
## 6 1 Big 5.958904 9 10 13 15
## 7 2 Big 5.898630 8 7 12 15
## behavioral ToM
## 1 37 14
## 2 36 11
## 3 39 6
## 5 39 8
## 6 41 19
## 7 40 15
## [1] 64 9
describitive statistic
#summary(newdata)
sd(newdata$Age);mean(newdata$Age);range(newdata$Age)
## [1] 0.8971403
## [1] 4.84131
## [1] 3.361644 6.268493
sd(newdata$ToM_Neutral);sd(newdata$ToM_Emapthy);
## [1] 1.983063
## [1] 2.311065
sd(newdata$affective);sd(newdata$cognitive);sd(newdata$behavioral)
## [1] 1.887656
## [1] 3.392591
## [1] 5.673315
aggregate(newdata[,4:8], by=list(Sex), FUN=mean)
## Group.1 ToM_Neutral ToM_Emapthy affective cognitive behavioral
## 1 1 4.482759 5.068966 12.34483 11.65517 35.89655
## 2 2 4.628571 4.457143 13.05714 12.60000 38.48571
aggregate(newdata[,4:8], by=list(Sex), FUN=sd)
## Group.1 ToM_Neutral ToM_Emapthy affective cognitive behavioral
## 1 1 2.063643 2.404327 2.040236 2.819269 5.440226
## 2 2 1.941584 2.227408 1.714006 3.790080 5.669171
T.test ToM
t.test(newdata$ToM_Neutral,newdata$ToM_Emapthy,var.equal = T)
##
## Two Sample t-test
##
## data: newdata$ToM_Neutral and newdata$ToM_Emapthy
## t = -0.45152, df = 126, p-value = 0.6524
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.9251831 0.5814331
## sample estimates:
## mean of x mean of y
## 4.562500 4.734375
standardize data
scaledata <- newdata
scaledata[,4:9]<-scale(scaledata[,4:9])
#cor(newdata[,3:9]);rcorr(as.matrix(newdata[,3:9]))
for ToM analysis
summary(lm.beta(lm(ToM_Neutral~Sex+Age,data=scaledata)))
##
## Call:
## lm(formula = ToM_Neutral ~ Sex + Age, data = scaledata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.68115 -0.58052 -0.07797 0.55717 2.10595
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) -2.75560 0.00000 0.68122 -4.045 0.000149 ***
## Sex 0.02193 0.01101 0.22054 0.099 0.921103
## Age 0.56218 0.50435 0.12334 4.558 2.54e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8771 on 61 degrees of freedom
## Multiple R-squared: 0.2551, Adjusted R-squared: 0.2306
## F-statistic: 10.44 on 2 and 61 DF, p-value: 0.0001258
summary(lm.beta(lm(ToM_Emapthy~Sex+Age,data=scaledata)))
##
## Call:
## lm(formula = ToM_Emapthy ~ Sex + Age, data = scaledata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.5133 -0.5561 -0.1020 0.3256 2.5132
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) -1.8555 0.0000 0.7041 -2.635 0.010647 *
## Sex -0.3090 -0.1550 0.2280 -1.355 0.180295
## Age 0.4820 0.4324 0.1275 3.781 0.000358 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9066 on 61 degrees of freedom
## Multiple R-squared: 0.2041, Adjusted R-squared: 0.178
## F-statistic: 7.823 on 2 and 61 DF, p-value: 0.0009457
test for moderation effect
## All No moderation
summary(lm.beta(fit1 <- lm(ToM_Neutral~Sex+Age,data=scaledata)))
##
## Call:
## lm(formula = ToM_Neutral ~ Sex + Age, data = scaledata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.68115 -0.58052 -0.07797 0.55717 2.10595
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) -2.75560 0.00000 0.68122 -4.045 0.000149 ***
## Sex 0.02193 0.01101 0.22054 0.099 0.921103
## Age 0.56218 0.50435 0.12334 4.558 2.54e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8771 on 61 degrees of freedom
## Multiple R-squared: 0.2551, Adjusted R-squared: 0.2306
## F-statistic: 10.44 on 2 and 61 DF, p-value: 0.0001258
#summary(lm.beta(fit1.2 <- lm(ToM_Neutral~Sex+Age+Sex:Age,data=scaledata)))
#anova(fit1,fit1.2)
summary(lm.beta(fit2 <- lm(ToM_Emapthy~Sex+Age,data=scaledata)))
##
## Call:
## lm(formula = ToM_Emapthy ~ Sex + Age, data = scaledata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.5133 -0.5561 -0.1020 0.3256 2.5132
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) -1.8555 0.0000 0.7041 -2.635 0.010647 *
## Sex -0.3090 -0.1550 0.2280 -1.355 0.180295
## Age 0.4820 0.4324 0.1275 3.781 0.000358 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9066 on 61 degrees of freedom
## Multiple R-squared: 0.2041, Adjusted R-squared: 0.178
## F-statistic: 7.823 on 2 and 61 DF, p-value: 0.0009457
#summary(lm.beta(fit2.2 <- lm(ToM_Emapthy~Sex+Age+Sex:Age,data=scaledata)))
#anova(fit2,fit2.2)
final plot
tomdata <- melt(newdata[,1:5],id=c("Sex","Class","Age"))
tomdata <- rename(tomdata,Condition=variable,Score = value)
#ggplot(tomdata,aes(x=Age,y=Score,col=Condition))+
# geom_point()+
# facet_grid(.~Sex)+
# geom_smooth(method="lm")+
# scale_colour_brewer(palette="Spectral")
ggplot(tomdata,aes(x=Age,y=Score,col=Condition))+
geom_point()+
#facet_grid(.~Sex)+
geom_smooth(method="lm")+
scale_colour_brewer(palette="Spectral")
ToM_N v.s ToM_Em
#ggplot(tomdata,aes(x=Score,fill=Condition,group=Condition))+
# #facet_grid(.~Sex)+
# geom_density(alpha = 0.5)+
# scale_fill_brewer(palette="Spectral")
#summary(lm.beta(lm(Score~Condition+Age+Sex,data=tomdata)))
Empathy Scale
emdata<- melt(scaledata,id=c("Sex","Class","Age","ToM_Neutral","ToM_Emapthy","ToM"))
emdata <- rename(emdata,Factor = variable,Score=value)
plot for three factors
ggplot(emdata,aes(x=Age,y=Score,group=Factor,col=Factor))+
#facet_grid(.~Factor)+
geom_point()+
geom_smooth(method="lm")+
scale_colour_brewer(palette="Spectral")
t.test for factors
# No interaction
summary(lm.beta(em1 <- lm(affective~Age+Sex,data=scaledata)))
##
## Call:
## lm(formula = affective ~ Age + Sex, data = scaledata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.8023 -0.6341 -0.1426 0.4274 2.7383
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) -2.6268 0.0000 0.7114 -3.692 0.000476 ***
## Age 0.4348 0.3900 0.1288 3.375 0.001287 **
## Sex 0.3375 0.1693 0.2303 1.465 0.148010
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.916 on 61 degrees of freedom
## Multiple R-squared: 0.1876, Adjusted R-squared: 0.1609
## F-statistic: 7.042 on 2 and 61 DF, p-value: 0.001772
#summary(lm.beta(em1.2 <- lm(affective~Age*Sex,data=scaledata)))
#anova(em1,em1.2)
summary(lm.beta(em2 <- lm(cognitive~Age+Sex,data=scaledata)))
##
## Call:
## lm(formula = cognitive ~ Age + Sex, data = scaledata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.53881 -0.39568 -0.04678 0.36450 1.87339
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) -4.26420 0.00000 0.52729 -8.087 3.12e-11 ***
## Age 0.81573 0.73182 0.09547 8.544 5.12e-12 ***
## Sex 0.20363 0.10217 0.17071 1.193 0.238
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6789 on 61 degrees of freedom
## Multiple R-squared: 0.5537, Adjusted R-squared: 0.539
## F-statistic: 37.84 on 2 and 61 DF, p-value: 2.062e-11
#summary(lm.beta(em2.2 <- lm(cognitive~Age*Sex,data=scaledata)))
#anova(em2,em2.2)
summary(lm.beta(em3 <- lm(behavioral~Age+Sex,data=scaledata)))
##
## Call:
## lm(formula = behavioral ~ Age + Sex, data = scaledata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.63496 -0.66377 -0.06556 0.59378 1.63190
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) -3.4629 0.0000 0.6467 -5.355 1.37e-06 ***
## Age 0.5867 0.5263 0.1171 5.010 4.94e-06 ***
## Sex 0.4025 0.2020 0.2094 1.923 0.0592 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8326 on 61 degrees of freedom
## Multiple R-squared: 0.3287, Adjusted R-squared: 0.3067
## F-statistic: 14.93 on 2 and 61 DF, p-value: 5.258e-06
#summary(lm.beta(em3.2 <- lm(behavioral~Age*Sex,data=scaledata)))
#anova(em3,em3.2)
cor(scaledata[,-c(1:3,9)]);corrplot.mixed(cor(scaledata[,-c(1:3,9)]))
## ToM_Neutral ToM_Emapthy affective cognitive behavioral
## ToM_Neutral 1.0000000 0.5041505 0.2822473 0.5374882 0.4468920
## ToM_Emapthy 0.5041505 1.0000000 0.1691342 0.4816706 0.4071490
## affective 0.2822473 0.1691342 1.0000000 0.6021044 0.6674423
## cognitive 0.5374882 0.4816706 0.6021044 1.0000000 0.7674261
## behavioral 0.4468920 0.4071490 0.6674423 0.7674261 1.0000000
canonical correlation for ToM and Empathy
#cancor
ToM <- scaledata[,4:5];Empathy <- scaledata[,6:8]
cc1 <- cancor(ToM,Empathy)
cc1
##
## Canonical correlation analysis of:
## 2 X variables: ToM_Neutral, ToM_Emapthy
## with 3 Y variables: affective, cognitive, behavioral
##
## CanR CanRSQ Eigen percent cum scree
## 1 0.6120 0.3745 0.59875 97.536 97.54 ******************************
## 2 0.1221 0.0149 0.01513 2.464 100.00 *
##
## Test of H0: The canonical correlations in the
## current row and all that follow are zero
##
## CanR WilksL F df1 df2 p.value
## 1 0.61197 0.61617 5.3876 6 118 5.9736e-05
## 2 0.12208 0.98510 2
coef(cc1,type="both",standardize = T)
## [[1]]
## Xcan1 Xcan2
## ToM_Neutral -0.6186142 0.9788261
## ToM_Emapthy -0.5334550 -1.0277207
##
## [[2]]
## Ycan1 Ycan2
## affective 0.3382339 1.31954876
## cognitive -0.9111864 0.04040218
## behavioral -0.3331341 -0.75613983
In order to aviod confounding from age and sex use residual to analyse
#USE residual
scaledata$ToM_Neutral_Re <- lm.beta(lm(ToM_Neutral~Age+Sex,data=scaledata))$residuals
scaledata$ToM_Emapthy_Re <- lm.beta(lm(ToM_Emapthy~Age+Sex,data=scaledata))$residuals
scaledata$affective_Re <- lm.beta(lm(affective~Age+Sex,data=scaledata))$residuals
scaledata$cognitive_Re <- lm.beta(lm(cognitive~Age+Sex,data=scaledata))$residuals
scaledata$behavioral_Re <- lm.beta(lm(behavioral~Age+Sex,data=scaledata))$residuals
ToM_Re <- scaledata[,10:11];Empathy_Re <- scaledata[,12:14]
cc2 <- cancor(ToM_Re,Empathy_Re)
cc2
##
## Canonical correlation analysis of:
## 2 X variables: ToM_Neutral_Re, ToM_Emapthy_Re
## with 3 Y variables: affective_Re, cognitive_Re, behavioral_Re
##
## CanR CanRSQ Eigen percent cum scree
## 1 0.41756 0.174359 0.211180 96.753 96.75 ******************************
## 2 0.08388 0.007036 0.007086 3.247 100.00 *
##
## Test of H0: The canonical correlations in the
## current row and all that follow are zero
##
## CanR WilksL F df1 df2 p.value
## 1 0.41756 0.81983 2.0538 6 118 0.063855
## 2 0.08388 0.99296 2
coef(cc2,type="both",standardize = T)
## [[1]]
## Xcan1 Xcan2
## ToM_Neutral_Re -0.4373948 0.9879471
## ToM_Emapthy_Re -0.7487808 -0.7788973
##
## [[2]]
## Ycan1 Ycan2
## affective_Re 0.5309051 1.0771660
## cognitive_Re -0.7144014 0.3571658
## behavioral_Re -0.6169153 -0.6231146
compare cc1 and cc2
#cc1
coef(cc1,type="both",standardize = T)
## [[1]]
## Xcan1 Xcan2
## ToM_Neutral -0.6186142 0.9788261
## ToM_Emapthy -0.5334550 -1.0277207
##
## [[2]]
## Ycan1 Ycan2
## affective 0.3382339 1.31954876
## cognitive -0.9111864 0.04040218
## behavioral -0.3331341 -0.75613983
#cc2
coef(cc2,type="both",standardize = T)
## [[1]]
## Xcan1 Xcan2
## ToM_Neutral_Re -0.4373948 0.9879471
## ToM_Emapthy_Re -0.7487808 -0.7788973
##
## [[2]]
## Ycan1 Ycan2
## affective_Re 0.5309051 1.0771660
## cognitive_Re -0.7144014 0.3571658
## behavioral_Re -0.6169153 -0.6231146
# combine 2 TOM data
summary(lm.beta(lm(ToM~cognitive+Age+Sex,data = scaledata)))
##
## Call:
## lm(formula = ToM ~ cognitive + Age + Sex, data = scaledata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.4255 -0.5918 -0.1005 0.4656 1.9750
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) -0.6881 0.0000 0.9004 -0.764 0.44773
## cognitive 0.4522 0.4522 0.1519 2.977 0.00419 **
## Age 0.2290 0.2055 0.1679 1.364 0.17754
## Sex -0.2720 -0.1365 0.2049 -1.328 0.18935
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8054 on 60 degrees of freedom
## Multiple R-squared: 0.3822, Adjusted R-squared: 0.3513
## F-statistic: 12.37 on 3 and 60 DF, p-value: 2.111e-06
#summary(lm.beta(lm(ToM~cognitive*Age+Sex,data = scaledata))) No interaction
summary(lm.beta(lm(ToM~affective+cognitive+behavioral+Age+Sex,data = scaledata)))
##
## Call:
## lm(formula = ToM ~ affective + cognitive + behavioral + Age +
## Sex, data = scaledata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.39635 -0.53713 -0.05487 0.45467 1.91693
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) -0.6307 0.0000 0.8933 -0.706 0.4830
## affective -0.1964 -0.1964 0.1381 -1.422 0.1603
## cognitive 0.3779 0.3779 0.1999 1.890 0.0637 .
## behavioral 0.2571 0.2571 0.1734 1.483 0.1436
## Age 0.2242 0.2012 0.1669 1.343 0.1844
## Sex -0.2941 -0.1475 0.2071 -1.420 0.1610
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7985 on 58 degrees of freedom
## Multiple R-squared: 0.4131, Adjusted R-squared: 0.3625
## F-statistic: 8.163 on 5 and 58 DF, p-value: 6.937e-06