#Descriptive analysis

library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
library(gapminder)
library(ggplot2)
library(ggthemes)
library(gridExtra)
library(compareGroups)
## Loading required package: SNPassoc
## Loading required package: haplo.stats
## Loading required package: survival
## Loading required package: mvtnorm
## Loading required package: parallel
## Registered S3 method overwritten by 'SNPassoc':
##   method            from       
##   summary.haplo.glm haplo.stats

#Reading data PISA

t="D:\\R ANALYSIS\\PISA Data Vietnam 2015.csv"
pisa=read.csv(t)
dim(pisa)
## [1] 5826   18

#Descriptive analysis with table1 and compareGroups

table1(~Region+Area+SchoolType+Math+Read+Science|Area,data=pisa)
REMOTE
(n=410)
RURAL
(n=2368)
URBAN
(n=3048)
Overall
(n=5826)
Region
CENTRAL 198 (48.3%) 857 (36.2%) 951 (31.2%) 2006 (34.4%)
NORTH 148 (36.1%) 764 (32.3%) 1046 (34.3%) 1958 (33.6%)
SOUTH 64 (15.6%) 747 (31.5%) 1051 (34.5%) 1862 (32.0%)
Area
REMOTE 410 (100%) 0 (0%) 0 (0%) 410 (7.0%)
RURAL 0 (0%) 2368 (100%) 0 (0%) 2368 (40.6%)
URBAN 0 (0%) 0 (0%) 3048 (100%) 3048 (52.3%)
SchoolType
Mean (SD) 3.00 (0.00) 2.89 (0.464) 2.80 (0.600) 2.85 (0.528)
Median [Min, Max] 3.00 [3.00, 3.00] 3.00 [1.00, 3.00] 3.00 [1.00, 3.00] 3.00 [1.00, 3.00]
Missing 0 (0%) 0 (0%) 35 (1.1%) 35 (0.6%)
Math
Mean (SD) 450 (82.0) 500 (81.9) 499 (79.3) 496 (81.5)
Median [Min, Max] 446 [216, 696] 498 [273, 818] 497 [202, 820] 493 [202, 820]
Read
Mean (SD) 440 (76.0) 491 (67.6) 496 (69.6) 490 (70.6)
Median [Min, Max] 439 [233, 643] 490 [292, 744] 495 [107, 718] 489 [107, 744]
Science
Mean (SD) 482 (74.4) 529 (75.5) 527 (72.8) 525 (75.0)
Median [Min, Max] 475 [307, 698] 529 [335, 807] 525 [293, 799] 524 [293, 807]
temp=compareGroups(Area~PARED+WEALTH+Math+Read+Science,data=pisa)
createTable(temp)
## 
## --------Summary descriptives table by 'Area'---------
## 
## ________________________________________________________ 
##            REMOTE       RURAL        URBAN     p.overall 
##            N=410        N=2368       N=3048              
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
## PARED   7.90 (3.69)  9.38 (3.47)  9.56 (3.48)   <0.001   
## WEALTH  -3.00 (1.25) -2.22 (1.08) -2.12 (1.16)  <0.001   
## Math     450 (82.0)   500 (81.9)   499 (79.3)   <0.001   
## Read     440 (76.0)   491 (67.6)   496 (69.6)   <0.001   
## Science  482 (74.4)   529 (75.5)   527 (72.8)   <0.001   
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

#Histogram

p3=ggplot(data=pisa,aes(x=Math,fill=Gender))
p3=p3+geom_histogram(position="dodge")
p3+labs(x="Math Score",y="Number of students")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Barplot - Part1

p=ggplot(data=pisa,aes(x=Area,fill=Area,col=Area))
p+geom_bar(position="dodge")

#Barplot - Part2

p=ggplot(data=pisa,aes(x=Gender,fill=Area,col=Area))
p+geom_bar(position="stack")

#Boxplot - Part1

p=ggplot(data=pisa,aes(x=Gender,y=Math,fill=Gender))
p+geom_boxplot()

#Boxplot - Part2

p=ggplot(data=pisa,aes(x=Gender,y=Math,fill=Gender))
p+geom_boxplot()+facet_grid(~Area)

#Scatter plot - Part1

p=ggplot(data=pisa,aes(x=WEALTH,y=Math,fill=Gender))
p+geom_point()
## Warning: Removed 15 rows containing missing values (geom_point).

#Scatter plot - Part2

p=ggplot(data=pisa,aes(x=WEALTH,y=Math,fill=Gender))
p+geom_point()+geom_smooth(method="lm",se=F)
## Warning: Removed 15 rows containing non-finite values (stat_smooth).
## Warning: Removed 15 rows containing missing values (geom_point).

#Scatter plot - Part3

p=ggplot(data=pisa,aes(x=WEALTH,y=Science,col=Area))
p+geom_point()+geom_smooth(method="lm",se=F)
## Warning: Removed 15 rows containing non-finite values (stat_smooth).
## Warning: Removed 15 rows containing missing values (geom_point).

#Multivariable correlation

library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
dat=pisa[,c("Area","Math","Science","Read")]
ggpairs(data=dat,mapping=aes(color=Area))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Multivariable correlation _Part2

library(GGally)
dat=pisa[,c("Area","Math","Science","Read","WEALTH")]
ggpairs(data=dat,mapping=aes(color=Area))
## Warning: Removed 15 rows containing non-finite values (stat_boxplot).
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning in (function (data, mapping, alignPercent = 0.6, method = "pearson", :
## Removed 15 rows containing missing values
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning in (function (data, mapping, alignPercent = 0.6, method = "pearson", :
## Removed 15 rows containing missing values
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning in (function (data, mapping, alignPercent = 0.6, method = "pearson", :
## Removed 15 rows containing missing values
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 15 rows containing non-finite values (stat_bin).
## Warning: Removed 15 rows containing missing values (geom_point).

## Warning: Removed 15 rows containing missing values (geom_point).

## Warning: Removed 15 rows containing missing values (geom_point).
## Warning: Removed 15 rows containing non-finite values (stat_density).