## time status sex age year thickness ulcer
## 1 10 3 1 76 1972 6.76 1
## 2 30 3 1 56 1968 0.65 0
## 3 35 2 1 41 1977 1.34 0
## 4 99 3 0 71 1968 2.90 0
## 5 185 1 1 52 1965 12.08 1
## 6 204 1 1 28 1971 4.84 1
##
## Pearson's product-moment correlation
##
## data: MASS::Melanoma$thickness and MASS::Melanoma$time
## t = -3.451, df = 203, p-value = 0.0006793
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.3608044 -0.1016529
## sample estimates:
## cor
## -0.2354087
##
## Welch Two Sample t-test
##
## data: MASS::Melanoma$time by MASS::Melanoma$ulcer
## t = 3.8629, df = 181.1, p-value = 0.0001559
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 292.1275 902.1807
## sample estimates:
## mean in group 0 mean in group 1
## 2414.965 1817.811
Quiz01
Quiz02
Quiz03
Quiz04
Quiz05
Quiz06
d<-read.csv("data.csv", head=TRUE)
summary (d)
## ID SEX AGE AREAdis
## Min. : 1.0 Min. :1.0 Min. :20.00 Min. : 0.0
## 1st Qu.:220.8 1st Qu.:1.0 1st Qu.:29.75 1st Qu.: 156.3
## Median :440.5 Median :1.5 Median :39.50 Median : 342.1
## Mean :440.5 Mean :1.5 Mean :39.68 Mean : 411.2
## 3rd Qu.:660.2 3rd Qu.:2.0 3rd Qu.:49.25 3rd Qu.: 594.8
## Max. :880.0 Max. :2.0 Max. :59.00 Max. :1756.4
## MARRIED CHILD Undesirable ConservatibeBuying
## Min. :1.000 Min. :1.000 Min. :1.00 Min. :1.000
## 1st Qu.:1.000 1st Qu.:1.000 1st Qu.:4.00 1st Qu.:2.000
## Median :2.000 Median :2.000 Median :5.00 Median :3.000
## Mean :1.583 Mean :1.503 Mean :4.58 Mean :3.422
## 3rd Qu.:2.000 3rd Qu.:2.000 3rd Qu.:6.00 3rd Qu.:4.000
## Max. :2.000 Max. :2.000 Max. :6.00 Max. :6.000
## FoodLiteracy01 FoodLiteracy02 FoodLiteracy03 FoodLiteracy04
## Min. :1.000 Min. :1.000 Min. :1.00 Min. :1.000
## 1st Qu.:3.000 1st Qu.:3.000 1st Qu.:2.00 1st Qu.:3.000
## Median :3.000 Median :3.000 Median :3.00 Median :3.000
## Mean :3.407 Mean :3.481 Mean :2.89 Mean :3.307
## 3rd Qu.:4.000 3rd Qu.:4.000 3rd Qu.:3.00 3rd Qu.:4.000
## Max. :5.000 Max. :5.000 Max. :5.00 Max. :5.000
## FoodLiteracy05 FoodLiteracy06 FoodLiteracy07 FoodLiteracy08
## Min. :1.000 Min. :1.000 Min. :1.000 Min. :1.000
## 1st Qu.:2.000 1st Qu.:2.000 1st Qu.:2.000 1st Qu.:3.000
## Median :3.000 Median :3.000 Median :3.000 Median :4.000
## Mean :3.067 Mean :2.893 Mean :3.034 Mean :3.689
## 3rd Qu.:4.000 3rd Qu.:4.000 3rd Qu.:4.000 3rd Qu.:5.000
## Max. :5.000 Max. :5.000 Max. :5.000 Max. :5.000
## FoodLiteracy09 Quiz01 Quiz02 Quiz03
## Min. :1.000 Min. :1.000 Min. :1.000 Min. :1.000
## 1st Qu.:2.000 1st Qu.:1.000 1st Qu.:1.000 1st Qu.:2.000
## Median :3.000 Median :3.000 Median :3.000 Median :3.000
## Mean :3.076 Mean :3.073 Mean :3.073 Mean :2.843
## 3rd Qu.:4.000 3rd Qu.:5.000 3rd Qu.:5.000 3rd Qu.:4.000
## Max. :5.000 Max. :5.000 Max. :5.000 Max. :5.000
## Quiz04 Quiz05 Quiz06
## Min. :1.00 Min. :1.00 Min. :1.000
## 1st Qu.:2.00 1st Qu.:3.00 1st Qu.:1.000
## Median :2.00 Median :4.00 Median :2.000
## Mean :2.65 Mean :3.78 Mean :2.717
## 3rd Qu.:5.00 3rd Qu.:5.00 3rd Qu.:5.000
## Max. :5.00 Max. :5.00 Max. :5.000
mean(d$AGE)
## [1] 39.67614
by (d, d$SEX, function(d) mean(d$AGE, na.rm=TRUE))
## d$SEX: 1
## [1] 39.85455
## --------------------------------------------------------
## d$SEX: 2
## [1] 39.49773
tapply(d$AGE, d$SEX, mean, na.rm=TRUE)
## 1 2
## 39.85455 39.49773
str(d)
## 'data.frame': 880 obs. of 23 variables:
## $ ID : int 1 2 3 4 5 6 7 8 9 10 ...
## $ SEX : int 1 1 1 1 1 1 2 1 2 1 ...
## $ AGE : int 53 54 48 59 52 39 55 58 41 43 ...
## $ AREAdis : num 196 946 0 0 141 ...
## $ MARRIED : int 2 2 2 2 1 2 2 2 1 2 ...
## $ CHILD : int 2 2 1 2 2 2 2 2 1 2 ...
## $ Undesirable : int 4 6 6 5 4 5 4 4 4 4 ...
## $ ConservatibeBuying: int 3 2 6 3 3 5 4 3 1 3 ...
## $ FoodLiteracy01 : int 3 4 5 1 4 3 4 3 4 4 ...
## $ FoodLiteracy02 : int 3 4 5 4 3 3 3 2 3 3 ...
## $ FoodLiteracy03 : int 3 3 5 2 3 3 3 2 3 3 ...
## $ FoodLiteracy04 : int 3 4 5 3 3 5 3 3 4 4 ...
## $ FoodLiteracy05 : int 3 3 5 2 3 3 3 2 4 4 ...
## $ FoodLiteracy06 : int 3 2 5 2 3 3 3 2 4 4 ...
## $ FoodLiteracy07 : int 3 2 5 2 3 3 3 2 4 3 ...
## $ FoodLiteracy08 : int 3 2 5 2 5 4 3 2 4 4 ...
## $ FoodLiteracy09 : int 3 2 5 2 1 3 3 2 3 4 ...
## $ Quiz01 : int 4 3 3 4 3 5 5 3 2 3 ...
## $ Quiz02 : int 1 1 3 1 1 5 5 5 1 1 ...
## $ Quiz03 : int 1 2 2 2 2 2 5 5 2 2 ...
## $ Quiz04 : int 2 1 1 2 1 2 5 2 5 5 ...
## $ Quiz05 : int 3 3 4 3 3 3 3 4 2 5 ...
## $ Quiz06 : int 2 3 2 1 1 5 5 2 1 2 ...
names(d)
## [1] "ID" "SEX" "AGE"
## [4] "AREAdis" "MARRIED" "CHILD"
## [7] "Undesirable" "ConservatibeBuying" "FoodLiteracy01"
## [10] "FoodLiteracy02" "FoodLiteracy03" "FoodLiteracy04"
## [13] "FoodLiteracy05" "FoodLiteracy06" "FoodLiteracy07"
## [16] "FoodLiteracy08" "FoodLiteracy09" "Quiz01"
## [19] "Quiz02" "Quiz03" "Quiz04"
## [22] "Quiz05" "Quiz06"
d$ID<-factor(d$ID)
d$SEX<-factor(d$SEX, labels=list("male", "female"))
d$MARRIED<-factor(d$MARRIED,labels=c("dontmarried","married"))
tapply(d$AGE, d$SEX, mean, na.rm=TRUE)
## male female
## 39.85455 39.49773
tapply(d$AGE, d$SEX, sd, na.rm=TRUE)
## male female
## 11.31418 11.06576
d$FoodLiteracy_all<-d$FoodLiteracy01+d$FoodLiteracy02+d$FoodLiteracy03+d$FoodLiteracy04
+d$FoodLiteracy05+d$FoodLiteracy06+d$FoodLiteracy07+d$FoodLiteracy08+d$FoodLiteracy09
hist(d$FoodLiteracy_all)
d$Quiz01A<-ifelse(d$Quiz01==3,1,0)
d$Quiz02A<-ifelse(d$Quiz02==4,1,0)
d$Quiz03A<-ifelse(d$Quiz03==3,1,0)
d$Quiz04A<-ifelse(d$Quiz04==1,1,0)
d$Quiz05A<-ifelse(d$Quiz05==3,1,0)
d$Quiz06A<-ifelse(d$Quiz06==2,1,0)
d$Quiz<-d$Quiz01A+d$Quiz02A+d$Quiz03A+d$Quiz04A+d$Quiz05A+d$Quiz06A
hist(d$Quiz)
plot(d$Quiz,d$FoodLiteracy_all)
library("ggplot2")
ggplot(d,aes(x=Quiz, y=FoodLiteracy_all, colour=SEX)) + geom_jitter(size=2)
cor.test(d$Quiz,d$FoodLiteracy_all)
##
## Pearson's product-moment correlation
##
## data: d$Quiz and d$FoodLiteracy_all
## t = -0.057895, df = 878, p-value = 0.9538
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.06803188 0.06414125
## sample estimates:
## cor
## -0.001953845
cor.test(d$Quiz,d$FoodLiteracy_all,method="spearman")
## Warning in cor.test.default(d$Quiz, d$FoodLiteracy_all, method =
## "spearman"): Cannot compute exact p-value with ties
##
## Spearman's rank correlation rho
##
## data: d$Quiz and d$FoodLiteracy_all
## S = 111130000, p-value = 0.523
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.02156133
library(psych)
corrvar<-data.frame(d$AGE,d$AREAdis,d$ConservatibeBuying,d$Undesirable,d$FoodLiteracy_all,d$Quiz)
corr.test(corrvar)
## Call:corr.test(x = corrvar)
## Correlation matrix
## d.AGE d.AREAdis d.ConservatibeBuying d.Undesirable
## d.AGE 1.00 0.01 0.01 0.01
## d.AREAdis 0.01 1.00 -0.07 -0.08
## d.ConservatibeBuying 0.01 -0.07 1.00 0.29
## d.Undesirable 0.01 -0.08 0.29 1.00
## d.FoodLiteracy_all 0.07 -0.02 -0.10 0.12
## d.Quiz 0.08 -0.10 0.14 0.14
## d.FoodLiteracy_all d.Quiz
## d.AGE 0.07 0.08
## d.AREAdis -0.02 -0.10
## d.ConservatibeBuying -0.10 0.14
## d.Undesirable 0.12 0.14
## d.FoodLiteracy_all 1.00 0.00
## d.Quiz 0.00 1.00
## Sample Size
## [1] 880
## Probability values (Entries above the diagonal are adjusted for multiple tests.)
## d.AGE d.AREAdis d.ConservatibeBuying d.Undesirable
## d.AGE 0.00 1.00 1.0 1.00
## d.AREAdis 0.82 0.00 0.3 0.11
## d.ConservatibeBuying 0.82 0.05 0.0 0.00
## d.Undesirable 0.69 0.01 0.0 0.00
## d.FoodLiteracy_all 0.03 0.49 0.0 0.00
## d.Quiz 0.02 0.00 0.0 0.00
## d.FoodLiteracy_all d.Quiz
## d.AGE 0.21 0.14
## d.AREAdis 1.00 0.03
## d.ConservatibeBuying 0.03 0.00
## d.Undesirable 0.00 0.00
## d.FoodLiteracy_all 0.00 1.00
## d.Quiz 0.95 0.00
##
## To see confidence intervals of the correlations, print with the short=FALSE option
library(corrplot)
corplot1<-cor(corrvar)
corrplot(corplot1)
t.test(d$Quiz~d$SEX)
##
## Welch Two Sample t-test
##
## data: d$Quiz by d$SEX
## t = 3.6787, df = 862.28, p-value = 0.0002489
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.1378203 0.4530888
## sample estimates:
## mean in group male mean in group female
## 1.454545 1.159091
tapply(d$Quiz, d$SEX, sd, na.rm=TRUE)
## male female
## 1.269128 1.107917
table(d$SEX)
##
## male female
## 440 440
library(compute.es)
result_t<-t.test(d$Quiz~d$SEX)
result_sd<-tapply(d$Quiz, d$SEX, sd, na.rm=TRUE)
result_table<-table(d$SEX)
mes(result_t$estimate[1],result_t$estimate[2],result_sd[1],result_sd[2],result_table[1],result_table[2])
## Mean Differences ES:
##
## d [ 95 %CI] = 0.25 [ 0.12 , 0.38 ]
## var(d) = 0
## p-value(d) = 0
## U3(d) = 59.79 %
## CLES(d) = 56.96 %
## Cliff's Delta = 0.14
##
## g [ 95 %CI] = 0.25 [ 0.12 , 0.38 ]
## var(g) = 0
## p-value(g) = 0
## U3(g) = 59.79 %
## CLES(g) = 56.95 %
##
## Correlation ES:
##
## r [ 95 %CI] = 0.12 [ 0.06 , 0.19 ]
## var(r) = 0
## p-value(r) = 0
##
## z [ 95 %CI] = 0.12 [ 0.06 , 0.19 ]
## var(z) = 0
## p-value(z) = 0
##
## Odds Ratio ES:
##
## OR [ 95 %CI] = 1.57 [ 1.23 , 2 ]
## p-value(OR) = 0
##
## Log OR [ 95 %CI] = 0.45 [ 0.21 , 0.69 ]
## var(lOR) = 0.02
## p-value(Log OR) = 0
##
## Other:
##
## NNT = 13.09
## Total N = 880
daihiko_t<-function(x,y,paired=TRUE) {
if (paired) {
d<-data.frame(x,y)
d<-subset(d,complete.cases(d))
rt<-t.test(d$x,d$y,paired=TRUE)
rsd<-c(sd(d$x,na.rm=T),sd(d$y,na.rm=T))
cohend<-abs(mean(d$x-d$y)/(sd(d$x-d$y)/sqrt(2*(1-cor(d$x,d$y)))))
return(list(meanx=mean(d$x),meany=mean(d$y),SD=rsd,t=rt,cohend=cohend))
}
else {
rt<-t.test(x~y)
rsd<-tapply(x,y,sd,na.rm=TRUE)
rt1<-table(y,x)
rt2<-apply(rt1,1,sum)
cohend<-(rt$estimate[1]-rt$estimate[2])/sqrt(((rt2[1]-1)*rsd[1]^2+(rt2[2]-1)*rsd[2]^2)/(rt2[1]+rt2[2]-2))
cohend<-unname(cohend)
return(list(N=rt2,SD=rsd,t=rt,cohend=cohend))
}
}
daihiko_t(d$Quiz,d$SEX,paired=FALSE)
## $N
## male female
## 440 440
##
## $SD
## male female
## 1.269128 1.107917
##
## $t
##
## Welch Two Sample t-test
##
## data: x by y
## t = 3.6787, df = 862.28, p-value = 0.0002489
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.1378203 0.4530888
## sample estimates:
## mean in group male mean in group female
## 1.454545 1.159091
##
##
## $cohend
## [1] 0.2480201
daihiko_t(d$FoodLiteracy01,d$FoodLiteracy02,paired=TRUE)
## $meanx
## [1] 3.406818
##
## $meany
## [1] 3.480682
##
## $SD
## [1] 1.0154434 0.9597542
##
## $t
##
## Paired t-test
##
## data: d$x and d$y
## t = -1.8344, df = 879, p-value = 0.06694
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.152893991 0.005166718
## sample estimates:
## mean of the differences
## -0.07386364
##
##
## $cohend
## [1] 0.07473953