##常用的指令都先loading
library(lattice)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(magrittr)
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:tidyr':
## 
##     extract
dta<- read.table("C:/tmp/brainsize.txt", h = T)
##看一下讀檔狀況
head(dta)
##   Sbj Gender FSIQ VIQ PIQ Weight Height MRICount
## 1   1 Female  133 132 124    118   64.5   816932
## 2   2   Male  140 150 124     NA   72.5  1001121
## 3   3   Male  139 123 150    143   73.3  1038437
## 4   4   Male  133 129 128    172   68.8   965353
## 5   5 Female  137 132 134    147   65.0   951545
## 6   6 Female   99  90 110    146   69.0   928799
##畫散佈圖
stripplot(FSIQ ~ PIQ | Gender,
        data=dta, 
        pch=1, 
        cex=.5, 
        alpha=.5,
        type=c('g','p'),
        jitter.data=TRUE,
        xlab="VIQ", 
        ylab='PIQ', 
        auto.key=list(space="top", 
                      columns=4),
        par.settings=standard.theme(color=FALSE))

##三種智力與男女性別差異p值均大於.05,男女之間智力未達顯著差異
t.test(dta$FSIQ ~ dta$Gender, paired=F, na.action = na.pass)
## 
##  Welch Two Sample t-test
## 
## data:  dta$FSIQ by dta$Gender
## t = -0.40267, df = 37.892, p-value = 0.6895
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -18.68639  12.48639
## sample estimates:
## mean in group Female   mean in group Male 
##                111.9                115.0
t.test(dta$VIQ ~ dta$Gender, paired=F, na.action = na.pass)
## 
##  Welch Two Sample t-test
## 
## data:  dta$VIQ by dta$Gender
## t = -0.77262, df = 36.973, p-value = 0.4447
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -21.010922   9.410922
## sample estimates:
## mean in group Female   mean in group Male 
##               109.45               115.25
t.test(dta$PIQ ~ dta$Gender, paired=F, na.action = na.pass)
## 
##  Welch Two Sample t-test
## 
## data:  dta$PIQ by dta$Gender
## t = -0.1598, df = 37.815, p-value = 0.8739
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -15.72079  13.42079
## sample estimates:
## mean in group Female   mean in group Male 
##               110.45               111.60
##選QQ圖看
qqmath( Weight~ Height | Gender, 
       aspect="xy", 
       data=dta,
       type=c('p','g'),
       prepanel=prepanel.qqmathline,
       panel=function(x, ...) {
         panel.qqmathline(x, ...)
         panel.qqmath(x, ...)
       },
       par.settings=standard.theme(color=FALSE))

##統計性別和身高體重關係,均有顯著影響力
summary(lm(dta$Weight ~ dta$Gender, data=dta, na.action = na.omit))
## 
## Call:
## lm(formula = dta$Weight ~ dta$Gender, data = dta, na.action = na.omit)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -34.444 -15.383   3.678  13.306  37.800 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     137.200      4.132  33.203  < 2e-16 ***
## dta$GenderMale   29.244      6.004   4.871 2.23e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 18.48 on 36 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.3972, Adjusted R-squared:  0.3805 
## F-statistic: 23.73 on 1 and 36 DF,  p-value: 2.227e-05
summary(lm(dta$Height ~ dta$Gender, data=dta, na.action = na.omit))
## 
## Call:
## lm(formula = dta$Height ~ dta$Gender, data = dta, na.action = na.omit)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -5.132 -2.432  0.235  2.152  5.568 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     65.7650     0.6298  104.42  < 2e-16 ***
## dta$GenderMale   5.6666     0.9023    6.28 2.62e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.816 on 37 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.516,  Adjusted R-squared:  0.5029 
## F-statistic: 39.44 on 1 and 37 DF,  p-value: 2.624e-07
##用圖來看性別、大腦大小、智商有沒有顯著影響力
xyplot(FSIQ ~ MRICount | Gender, 
       data=dta, 
       type="smooth",
       panel=function(x, y, ...) {
         panel.xyplot(x, y, ...)
         panel.grid(h=-1, 
                    v=-1, 
                    col="gray80", 
                    lty=3, ...)
         panel.average(x, y, fun=mean, 
                       horizontal=FALSE, 
                       col='gray', ...)},
       par.settings=standard.theme(color=FALSE))

##不同性別大腦大小有顯著影響力,但性別與智力沒有影響力
summary(lm(dta$MRICount ~ dta$Gender, data=dta, na.action = na.omit))
## 
## Call:
## lm(formula = dta$MRICount ~ dta$Gender, data = dta, na.action = na.omit)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -74868 -34593  -7290  20014 128650 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      862655      12500  69.011  < 2e-16 ***
## dta$GenderMale    92201      17678   5.216 6.76e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 55900 on 38 degrees of freedom
## Multiple R-squared:  0.4172, Adjusted R-squared:  0.4019 
## F-statistic:  27.2 on 1 and 38 DF,  p-value: 6.758e-06
summary(lm(dta$FSIQ ~ dta$Gender, data=dta, na.action = na.omit))
## 
## Call:
## lm(formula = dta$FSIQ ~ dta$Gender, data = dta, na.action = na.omit)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -35.00 -24.18   3.55  23.32  29.00 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     111.900      5.444  20.556   <2e-16 ***
## dta$GenderMale    3.100      7.699   0.403    0.689    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 24.34 on 38 degrees of freedom
## Multiple R-squared:  0.004249,   Adjusted R-squared:  -0.02196 
## F-statistic: 0.1621 on 1 and 38 DF,  p-value: 0.6894