16 April 2020

Pengenalan R, RStudio, ggplot, PCA, Git, sogosurvey.com

Pengenalan R, RStudio, ggplot, PCA, Git, sogosurvey.com
#package
knitr::opts_chunk$set(echo = TRUE)
library(knitr) # untuk R markdown
library(Rmisc) # untuk fungsi summarySE
## Loading required package: lattice
## Loading required package: plyr
library(agricolae)# untuk fungsi HSD.test
library(ggplot2) #untuk fungsi grafik dengna ggplot
library(cowplot) #untuk membuat grafik grid
## 
## ********************************************************
## Note: As of version 1.0.0, cowplot does not change the
##   default ggplot2 theme anymore. To recover the previous
##   behavior, execute:
##   theme_set(theme_cowplot())
## ********************************************************
library(rstatix ) #untuk fungsi uji normalitas shapiro wilk
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:plyr':
## 
##     mutate
## The following object is masked from 'package:stats':
## 
##     filter
library(ggpubr) #untuk membuat density plot uji normalitas
## Loading required package: magrittr
## 
## Attaching package: 'ggpubr'
## The following object is masked from 'package:cowplot':
## 
##     get_legend
## The following object is masked from 'package:plyr':
## 
##     mutate
library(dplyr) #fungsi kalkulasi untuk membuat boxplot
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr) #fungsi kalkulasi untuk membuat boxplot
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:magrittr':
## 
##     extract
library(ggfortify) #untuk membuat pca dengan ggplot
#data
data(ToothGrowth)
ToothGrowth
##     len supp dose
## 1   4.2   VC  0.5
## 2  11.5   VC  0.5
## 3   7.3   VC  0.5
## 4   5.8   VC  0.5
## 5   6.4   VC  0.5
## 6  10.0   VC  0.5
## 7  11.2   VC  0.5
## 8  11.2   VC  0.5
## 9   5.2   VC  0.5
## 10  7.0   VC  0.5
## 11 16.5   VC  1.0
## 12 16.5   VC  1.0
## 13 15.2   VC  1.0
## 14 17.3   VC  1.0
## 15 22.5   VC  1.0
## 16 17.3   VC  1.0
## 17 13.6   VC  1.0
## 18 14.5   VC  1.0
## 19 18.8   VC  1.0
## 20 15.5   VC  1.0
## 21 23.6   VC  2.0
## 22 18.5   VC  2.0
## 23 33.9   VC  2.0
## 24 25.5   VC  2.0
## 25 26.4   VC  2.0
## 26 32.5   VC  2.0
## 27 26.7   VC  2.0
## 28 21.5   VC  2.0
## 29 23.3   VC  2.0
## 30 29.5   VC  2.0
## 31 15.2   OJ  0.5
## 32 21.5   OJ  0.5
## 33 17.6   OJ  0.5
## 34  9.7   OJ  0.5
## 35 14.5   OJ  0.5
## 36 10.0   OJ  0.5
## 37  8.2   OJ  0.5
## 38  9.4   OJ  0.5
## 39 16.5   OJ  0.5
## 40  9.7   OJ  0.5
## 41 19.7   OJ  1.0
## 42 23.3   OJ  1.0
## 43 23.6   OJ  1.0
## 44 26.4   OJ  1.0
## 45 20.0   OJ  1.0
## 46 25.2   OJ  1.0
## 47 25.8   OJ  1.0
## 48 21.2   OJ  1.0
## 49 14.5   OJ  1.0
## 50 27.3   OJ  1.0
## 51 25.5   OJ  2.0
## 52 26.4   OJ  2.0
## 53 22.4   OJ  2.0
## 54 24.5   OJ  2.0
## 55 24.8   OJ  2.0
## 56 30.9   OJ  2.0
## 57 26.4   OJ  2.0
## 58 27.3   OJ  2.0
## 59 29.4   OJ  2.0
## 60 23.0   OJ  2.0

summarySE

ToothGrowth_sum<-summarySE(data = ToothGrowth, "len", groupvars = c("supp", "dose"), na.rm = FALSE, 
          conf.interval = 0.95, .drop = TRUE)
ToothGrowth_sum
##   supp dose  N   len       sd        se       ci
## 1   OJ  0.5 10 13.23 4.459709 1.4102837 3.190283
## 2   OJ  1.0 10 22.70 3.910953 1.2367520 2.797727
## 3   OJ  2.0 10 26.06 2.655058 0.8396031 1.899314
## 4   VC  0.5 10  7.98 2.746634 0.8685620 1.964824
## 5   VC  1.0 10 16.77 2.515309 0.7954104 1.799343
## 6   VC  2.0 10 26.14 4.797731 1.5171757 3.432090
str(ToothGrowth)
## 'data.frame':    60 obs. of  3 variables:
##  $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
##  $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
##  $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...

statistik

##linier model
tx_supp_dose <- with(ToothGrowth, interaction(supp, dose))
lm_supp_dose <- lm(len~tx_supp_dose, data = ToothGrowth)
summary(lm_supp_dose)
## 
## Call:
## lm(formula = len ~ tx_supp_dose, data = ToothGrowth)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##  -8.20  -2.72  -0.27   2.65   8.27 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          13.230      1.148  11.521 3.60e-16 ***
## tx_supp_doseVC.0.5   -5.250      1.624  -3.233  0.00209 ** 
## tx_supp_doseOJ.1      9.470      1.624   5.831 3.18e-07 ***
## tx_supp_doseVC.1      3.540      1.624   2.180  0.03365 *  
## tx_supp_doseOJ.2     12.830      1.624   7.900 1.43e-10 ***
## tx_supp_doseVC.2     12.910      1.624   7.949 1.19e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.631 on 54 degrees of freedom
## Multiple R-squared:  0.7937, Adjusted R-squared:  0.7746 
## F-statistic: 41.56 on 5 and 54 DF,  p-value: < 2.2e-16
##anova test
anova(lm_supp_dose)
## Analysis of Variance Table
## 
## Response: len
##              Df  Sum Sq Mean Sq F value    Pr(>F)    
## tx_supp_dose  5 2740.10  548.02  41.557 < 2.2e-16 ***
## Residuals    54  712.11   13.19                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##HSD test group
HSD_supp_dose<-HSD.test(lm_supp_dose, trt = "tx_supp_dose", group = TRUE, console=TRUE)
## 
## Study: lm_supp_dose ~ "tx_supp_dose"
## 
## HSD Test for len 
## 
## Mean Square Error:  13.18715 
## 
## tx_supp_dose,  means
## 
##          len      std  r  Min  Max
## OJ.0.5 13.23 4.459709 10  8.2 21.5
## OJ.1   22.70 3.910953 10 14.5 27.3
## OJ.2   26.06 2.655058 10 22.4 30.9
## VC.0.5  7.98 2.746634 10  4.2 11.5
## VC.1   16.77 2.515309 10 13.6 22.5
## VC.2   26.14 4.797731 10 18.5 33.9
## 
## Alpha: 0.05 ; DF Error: 54 
## Critical Value of Studentized Range: 4.178265 
## 
## Minimun Significant Difference: 4.798124 
## 
## Treatments with the same letter are not significantly different.
## 
##          len groups
## VC.2   26.14      a
## OJ.2   26.06      a
## OJ.1   22.70      a
## VC.1   16.77      b
## OJ.0.5 13.23      b
## VC.0.5  7.98      c

HSD test group p value

HSD_supp_doseP<-HSD.test(lm_supp_dose, trt = "tx_supp_dose", group = FALSE, console=TRUE)
## 
## Study: lm_supp_dose ~ "tx_supp_dose"
## 
## HSD Test for len 
## 
## Mean Square Error:  13.18715 
## 
## tx_supp_dose,  means
## 
##          len      std  r  Min  Max
## OJ.0.5 13.23 4.459709 10  8.2 21.5
## OJ.1   22.70 3.910953 10 14.5 27.3
## OJ.2   26.06 2.655058 10 22.4 30.9
## VC.0.5  7.98 2.746634 10  4.2 11.5
## VC.1   16.77 2.515309 10 13.6 22.5
## VC.2   26.14 4.797731 10 18.5 33.9
## 
## Alpha: 0.05 ; DF Error: 54 
## Critical Value of Studentized Range: 4.178265 
## 
## Comparison between treatments means
## 
##                 difference pvalue signif.         LCL        UCL
## OJ.0.5 - OJ.1        -9.47 0.0000     *** -14.2681238  -4.671876
## OJ.0.5 - OJ.2       -12.83 0.0000     *** -17.6281238  -8.031876
## OJ.0.5 - VC.0.5       5.25 0.0243       *   0.4518762  10.048124
## OJ.0.5 - VC.1        -3.54 0.2640          -8.3381238   1.258124
## OJ.0.5 - VC.2       -12.91 0.0000     *** -17.7081238  -8.111876
## OJ.1 - OJ.2          -3.36 0.3187          -8.1581238   1.438124
## OJ.1 - VC.0.5        14.72 0.0000     ***   9.9218762  19.518124
## OJ.1 - VC.1           5.93 0.0074      **   1.1318762  10.728124
## OJ.1 - VC.2          -3.44 0.2936          -8.2381238   1.358124
## OJ.2 - VC.0.5        18.08 0.0000     ***  13.2818762  22.878124
## OJ.2 - VC.1           9.29 0.0000     ***   4.4918762  14.088124
## OJ.2 - VC.2          -0.08 1.0000          -4.8781238   4.718124
## VC.0.5 - VC.1        -8.79 0.0000     *** -13.5881238  -3.991876
## VC.0.5 - VC.2       -18.16 0.0000     *** -22.9581238 -13.361876
## VC.1 - VC.2          -9.37 0.0000     *** -14.1681238  -4.571876

grafik

base

plot(ToothGrowth_sum$dose,ToothGrowth_sum$len)

#fungsi plot dasar pada R tidak dapat melakukan gruping, sehingga jenis suplemen tidak dapat diamati
##grafik ggplot ###grafik ggplot dalam 1 plot
g.ToothGrowth<- 
  ggplot(data = ToothGrowth_sum,aes(x = dose,y=len), na.rm = FALSE) +
    geom_point(data = ToothGrowth_sum, aes(color=supp), size=4)+
  theme_classic(base_size = 14)+
  xlab("Dose (mg)") +
  ylab("Length (mm)")
 g.ToothGrowth

###grafik ggplot dalam 2 plot bersusun
g.ToothGrowth_grid<- 
  ggplot(data = ToothGrowth_sum,aes(x = dose,y=len), na.rm = FALSE) +
    geom_point()+
  theme_classic(base_size = 14)+
  xlab("Dose (mg)") +
  ylab("Length (mm)")+
  facet_grid(cols = vars(supp))
 g.ToothGrowth_grid

###grafik ggplot dalam 2 plot manual
g.ToothGrowth_OJ<- 
  ggplot(data = ToothGrowth_sum[ToothGrowth_sum$supp=="OJ",],aes(x = dose,y=len), na.rm = FALSE) +
    geom_point()+
  theme_classic(base_size = 14)+
  xlab("Dose (mg)") +
  ylab("Length (mm)")
g.ToothGrowth_OJ

g.ToothGrowth_VC<- 
  ggplot(data = ToothGrowth_sum[ToothGrowth_sum$supp=="VC",],aes(x = dose,y=len), na.rm = FALSE) +
    geom_line()+
  geom_point()+
  theme_classic(base_size = 14)+
  xlab("Dose (mg)") +
  ylab("Length (mm)")
g.ToothGrowth_VC

g.ToothGrowth_grid<-plot_grid(g.ToothGrowth_OJ, g.ToothGrowth_VC,                       ncol=2,  align = 'v', rel_heights = c(1/5, 1/5),
                           labels = c('A', 'B'))
g.ToothGrowth_grid

ggsave("g.ToothGrowth_grid.pdf", plot= g.ToothGrowth_grid,  width = 200, height = 130, units = "mm")
#untuk menyimpan dalam bentuk pdf
ggsave("g.ToothGrowth_grid.png", plot= g.ToothGrowth_grid,  width = 200, height = 130, units = "mm")
#untuk menyimpan dalam bentuk png
rm(g.ToothGrowth_OV)
## Warning in rm(g.ToothGrowth_OV): object 'g.ToothGrowth_OV' not found
#PCA ##data
#data
data("iris")
iris
##     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1            5.1         3.5          1.4         0.2     setosa
## 2            4.9         3.0          1.4         0.2     setosa
## 3            4.7         3.2          1.3         0.2     setosa
## 4            4.6         3.1          1.5         0.2     setosa
## 5            5.0         3.6          1.4         0.2     setosa
## 6            5.4         3.9          1.7         0.4     setosa
## 7            4.6         3.4          1.4         0.3     setosa
## 8            5.0         3.4          1.5         0.2     setosa
## 9            4.4         2.9          1.4         0.2     setosa
## 10           4.9         3.1          1.5         0.1     setosa
## 11           5.4         3.7          1.5         0.2     setosa
## 12           4.8         3.4          1.6         0.2     setosa
## 13           4.8         3.0          1.4         0.1     setosa
## 14           4.3         3.0          1.1         0.1     setosa
## 15           5.8         4.0          1.2         0.2     setosa
## 16           5.7         4.4          1.5         0.4     setosa
## 17           5.4         3.9          1.3         0.4     setosa
## 18           5.1         3.5          1.4         0.3     setosa
## 19           5.7         3.8          1.7         0.3     setosa
## 20           5.1         3.8          1.5         0.3     setosa
## 21           5.4         3.4          1.7         0.2     setosa
## 22           5.1         3.7          1.5         0.4     setosa
## 23           4.6         3.6          1.0         0.2     setosa
## 24           5.1         3.3          1.7         0.5     setosa
## 25           4.8         3.4          1.9         0.2     setosa
## 26           5.0         3.0          1.6         0.2     setosa
## 27           5.0         3.4          1.6         0.4     setosa
## 28           5.2         3.5          1.5         0.2     setosa
## 29           5.2         3.4          1.4         0.2     setosa
## 30           4.7         3.2          1.6         0.2     setosa
## 31           4.8         3.1          1.6         0.2     setosa
## 32           5.4         3.4          1.5         0.4     setosa
## 33           5.2         4.1          1.5         0.1     setosa
## 34           5.5         4.2          1.4         0.2     setosa
## 35           4.9         3.1          1.5         0.2     setosa
## 36           5.0         3.2          1.2         0.2     setosa
## 37           5.5         3.5          1.3         0.2     setosa
## 38           4.9         3.6          1.4         0.1     setosa
## 39           4.4         3.0          1.3         0.2     setosa
## 40           5.1         3.4          1.5         0.2     setosa
## 41           5.0         3.5          1.3         0.3     setosa
## 42           4.5         2.3          1.3         0.3     setosa
## 43           4.4         3.2          1.3         0.2     setosa
## 44           5.0         3.5          1.6         0.6     setosa
## 45           5.1         3.8          1.9         0.4     setosa
## 46           4.8         3.0          1.4         0.3     setosa
## 47           5.1         3.8          1.6         0.2     setosa
## 48           4.6         3.2          1.4         0.2     setosa
## 49           5.3         3.7          1.5         0.2     setosa
## 50           5.0         3.3          1.4         0.2     setosa
## 51           7.0         3.2          4.7         1.4 versicolor
## 52           6.4         3.2          4.5         1.5 versicolor
## 53           6.9         3.1          4.9         1.5 versicolor
## 54           5.5         2.3          4.0         1.3 versicolor
## 55           6.5         2.8          4.6         1.5 versicolor
## 56           5.7         2.8          4.5         1.3 versicolor
## 57           6.3         3.3          4.7         1.6 versicolor
## 58           4.9         2.4          3.3         1.0 versicolor
## 59           6.6         2.9          4.6         1.3 versicolor
## 60           5.2         2.7          3.9         1.4 versicolor
## 61           5.0         2.0          3.5         1.0 versicolor
## 62           5.9         3.0          4.2         1.5 versicolor
## 63           6.0         2.2          4.0         1.0 versicolor
## 64           6.1         2.9          4.7         1.4 versicolor
## 65           5.6         2.9          3.6         1.3 versicolor
## 66           6.7         3.1          4.4         1.4 versicolor
## 67           5.6         3.0          4.5         1.5 versicolor
## 68           5.8         2.7          4.1         1.0 versicolor
## 69           6.2         2.2          4.5         1.5 versicolor
## 70           5.6         2.5          3.9         1.1 versicolor
## 71           5.9         3.2          4.8         1.8 versicolor
## 72           6.1         2.8          4.0         1.3 versicolor
## 73           6.3         2.5          4.9         1.5 versicolor
## 74           6.1         2.8          4.7         1.2 versicolor
## 75           6.4         2.9          4.3         1.3 versicolor
## 76           6.6         3.0          4.4         1.4 versicolor
## 77           6.8         2.8          4.8         1.4 versicolor
## 78           6.7         3.0          5.0         1.7 versicolor
## 79           6.0         2.9          4.5         1.5 versicolor
## 80           5.7         2.6          3.5         1.0 versicolor
## 81           5.5         2.4          3.8         1.1 versicolor
## 82           5.5         2.4          3.7         1.0 versicolor
## 83           5.8         2.7          3.9         1.2 versicolor
## 84           6.0         2.7          5.1         1.6 versicolor
## 85           5.4         3.0          4.5         1.5 versicolor
## 86           6.0         3.4          4.5         1.6 versicolor
## 87           6.7         3.1          4.7         1.5 versicolor
## 88           6.3         2.3          4.4         1.3 versicolor
## 89           5.6         3.0          4.1         1.3 versicolor
## 90           5.5         2.5          4.0         1.3 versicolor
## 91           5.5         2.6          4.4         1.2 versicolor
## 92           6.1         3.0          4.6         1.4 versicolor
## 93           5.8         2.6          4.0         1.2 versicolor
## 94           5.0         2.3          3.3         1.0 versicolor
## 95           5.6         2.7          4.2         1.3 versicolor
## 96           5.7         3.0          4.2         1.2 versicolor
## 97           5.7         2.9          4.2         1.3 versicolor
## 98           6.2         2.9          4.3         1.3 versicolor
## 99           5.1         2.5          3.0         1.1 versicolor
## 100          5.7         2.8          4.1         1.3 versicolor
## 101          6.3         3.3          6.0         2.5  virginica
## 102          5.8         2.7          5.1         1.9  virginica
## 103          7.1         3.0          5.9         2.1  virginica
## 104          6.3         2.9          5.6         1.8  virginica
## 105          6.5         3.0          5.8         2.2  virginica
## 106          7.6         3.0          6.6         2.1  virginica
## 107          4.9         2.5          4.5         1.7  virginica
## 108          7.3         2.9          6.3         1.8  virginica
## 109          6.7         2.5          5.8         1.8  virginica
## 110          7.2         3.6          6.1         2.5  virginica
## 111          6.5         3.2          5.1         2.0  virginica
## 112          6.4         2.7          5.3         1.9  virginica
## 113          6.8         3.0          5.5         2.1  virginica
## 114          5.7         2.5          5.0         2.0  virginica
## 115          5.8         2.8          5.1         2.4  virginica
## 116          6.4         3.2          5.3         2.3  virginica
## 117          6.5         3.0          5.5         1.8  virginica
## 118          7.7         3.8          6.7         2.2  virginica
## 119          7.7         2.6          6.9         2.3  virginica
## 120          6.0         2.2          5.0         1.5  virginica
## 121          6.9         3.2          5.7         2.3  virginica
## 122          5.6         2.8          4.9         2.0  virginica
## 123          7.7         2.8          6.7         2.0  virginica
## 124          6.3         2.7          4.9         1.8  virginica
## 125          6.7         3.3          5.7         2.1  virginica
## 126          7.2         3.2          6.0         1.8  virginica
## 127          6.2         2.8          4.8         1.8  virginica
## 128          6.1         3.0          4.9         1.8  virginica
## 129          6.4         2.8          5.6         2.1  virginica
## 130          7.2         3.0          5.8         1.6  virginica
## 131          7.4         2.8          6.1         1.9  virginica
## 132          7.9         3.8          6.4         2.0  virginica
## 133          6.4         2.8          5.6         2.2  virginica
## 134          6.3         2.8          5.1         1.5  virginica
## 135          6.1         2.6          5.6         1.4  virginica
## 136          7.7         3.0          6.1         2.3  virginica
## 137          6.3         3.4          5.6         2.4  virginica
## 138          6.4         3.1          5.5         1.8  virginica
## 139          6.0         3.0          4.8         1.8  virginica
## 140          6.9         3.1          5.4         2.1  virginica
## 141          6.7         3.1          5.6         2.4  virginica
## 142          6.9         3.1          5.1         2.3  virginica
## 143          5.8         2.7          5.1         1.9  virginica
## 144          6.8         3.2          5.9         2.3  virginica
## 145          6.7         3.3          5.7         2.5  virginica
## 146          6.7         3.0          5.2         2.3  virginica
## 147          6.3         2.5          5.0         1.9  virginica
## 148          6.5         3.0          5.2         2.0  virginica
## 149          6.2         3.4          5.4         2.3  virginica
## 150          5.9         3.0          5.1         1.8  virginica

data profile

iris %>% shapiro_test(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
## # A tibble: 4 x 3
##   variable     statistic        p
##   <chr>            <dbl>    <dbl>
## 1 Petal.Length     0.876 7.41e-10
## 2 Petal.Width      0.902 1.68e- 8
## 3 Sepal.Length     0.976 1.02e- 2
## 4 Sepal.Width      0.985 1.01e- 1
#  p-value > 0.05 implying that the distribution of the data are not significantly different from normal distribution. In other words, we can assume the normality.


g.density_Sepal.Length<-ggdensity(iris$Sepal.Length, 
          main = "Density plot of Sepal Length",
          xlab = "Sepal Length")

g.density_Sepal.Width<-ggdensity(iris$Sepal.Width, 
          main = "Density plot of Sepal Width",
          xlab = "Sepal Width")

g.density_Petal.Length<-ggdensity(iris$Petal.Length, 
          main = "Density plot of Petal Length",
          xlab = "Petal Length")

g.density_Petal.Width<-ggdensity(iris$Petal.Width, 
          main = "Density plot of Petal Width",
          xlab = "Petal Width")


g.density_iris_grid<-plot_grid(g.density_Petal.Length, g.density_Petal.Width, g.density_Sepal.Length, g.density_Sepal.Width,                       ncol=2,  align = 'v', rel_heights = c(1/5, 1/5, 1/5, 1/5),
                           labels = c('A', 'B', 'C', 'D'))
g.density_iris_grid

##data transformation ###log transformation
#https://www.r-bloggers.com/computing-and-visualizing-pca-in-r/
#menggunakan log transformation, scaling and mean centering transformation
log.ir <- log(iris[, 1:4])
ir.species <- iris[, 5]
log.ir
##     Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1       1.629241   1.2527630   0.33647224 -1.60943791
## 2       1.589235   1.0986123   0.33647224 -1.60943791
## 3       1.547563   1.1631508   0.26236426 -1.60943791
## 4       1.526056   1.1314021   0.40546511 -1.60943791
## 5       1.609438   1.2809338   0.33647224 -1.60943791
## 6       1.686399   1.3609766   0.53062825 -0.91629073
## 7       1.526056   1.2237754   0.33647224 -1.20397280
## 8       1.609438   1.2237754   0.40546511 -1.60943791
## 9       1.481605   1.0647107   0.33647224 -1.60943791
## 10      1.589235   1.1314021   0.40546511 -2.30258509
## 11      1.686399   1.3083328   0.40546511 -1.60943791
## 12      1.568616   1.2237754   0.47000363 -1.60943791
## 13      1.568616   1.0986123   0.33647224 -2.30258509
## 14      1.458615   1.0986123   0.09531018 -2.30258509
## 15      1.757858   1.3862944   0.18232156 -1.60943791
## 16      1.740466   1.4816045   0.40546511 -0.91629073
## 17      1.686399   1.3609766   0.26236426 -0.91629073
## 18      1.629241   1.2527630   0.33647224 -1.20397280
## 19      1.740466   1.3350011   0.53062825 -1.20397280
## 20      1.629241   1.3350011   0.40546511 -1.20397280
## 21      1.686399   1.2237754   0.53062825 -1.60943791
## 22      1.629241   1.3083328   0.40546511 -0.91629073
## 23      1.526056   1.2809338   0.00000000 -1.60943791
## 24      1.629241   1.1939225   0.53062825 -0.69314718
## 25      1.568616   1.2237754   0.64185389 -1.60943791
## 26      1.609438   1.0986123   0.47000363 -1.60943791
## 27      1.609438   1.2237754   0.47000363 -0.91629073
## 28      1.648659   1.2527630   0.40546511 -1.60943791
## 29      1.648659   1.2237754   0.33647224 -1.60943791
## 30      1.547563   1.1631508   0.47000363 -1.60943791
## 31      1.568616   1.1314021   0.47000363 -1.60943791
## 32      1.686399   1.2237754   0.40546511 -0.91629073
## 33      1.648659   1.4109870   0.40546511 -2.30258509
## 34      1.704748   1.4350845   0.33647224 -1.60943791
## 35      1.589235   1.1314021   0.40546511 -1.60943791
## 36      1.609438   1.1631508   0.18232156 -1.60943791
## 37      1.704748   1.2527630   0.26236426 -1.60943791
## 38      1.589235   1.2809338   0.33647224 -2.30258509
## 39      1.481605   1.0986123   0.26236426 -1.60943791
## 40      1.629241   1.2237754   0.40546511 -1.60943791
## 41      1.609438   1.2527630   0.26236426 -1.20397280
## 42      1.504077   0.8329091   0.26236426 -1.20397280
## 43      1.481605   1.1631508   0.26236426 -1.60943791
## 44      1.609438   1.2527630   0.47000363 -0.51082562
## 45      1.629241   1.3350011   0.64185389 -0.91629073
## 46      1.568616   1.0986123   0.33647224 -1.20397280
## 47      1.629241   1.3350011   0.47000363 -1.60943791
## 48      1.526056   1.1631508   0.33647224 -1.60943791
## 49      1.667707   1.3083328   0.40546511 -1.60943791
## 50      1.609438   1.1939225   0.33647224 -1.60943791
## 51      1.945910   1.1631508   1.54756251  0.33647224
## 52      1.856298   1.1631508   1.50407740  0.40546511
## 53      1.931521   1.1314021   1.58923521  0.40546511
## 54      1.704748   0.8329091   1.38629436  0.26236426
## 55      1.871802   1.0296194   1.52605630  0.40546511
## 56      1.740466   1.0296194   1.50407740  0.26236426
## 57      1.840550   1.1939225   1.54756251  0.47000363
## 58      1.589235   0.8754687   1.19392247  0.00000000
## 59      1.887070   1.0647107   1.52605630  0.26236426
## 60      1.648659   0.9932518   1.36097655  0.33647224
## 61      1.609438   0.6931472   1.25276297  0.00000000
## 62      1.774952   1.0986123   1.43508453  0.40546511
## 63      1.791759   0.7884574   1.38629436  0.00000000
## 64      1.808289   1.0647107   1.54756251  0.33647224
## 65      1.722767   1.0647107   1.28093385  0.26236426
## 66      1.902108   1.1314021   1.48160454  0.33647224
## 67      1.722767   1.0986123   1.50407740  0.40546511
## 68      1.757858   0.9932518   1.41098697  0.00000000
## 69      1.824549   0.7884574   1.50407740  0.40546511
## 70      1.722767   0.9162907   1.36097655  0.09531018
## 71      1.774952   1.1631508   1.56861592  0.58778666
## 72      1.808289   1.0296194   1.38629436  0.26236426
## 73      1.840550   0.9162907   1.58923521  0.40546511
## 74      1.808289   1.0296194   1.54756251  0.18232156
## 75      1.856298   1.0647107   1.45861502  0.26236426
## 76      1.887070   1.0986123   1.48160454  0.33647224
## 77      1.916923   1.0296194   1.56861592  0.33647224
## 78      1.902108   1.0986123   1.60943791  0.53062825
## 79      1.791759   1.0647107   1.50407740  0.40546511
## 80      1.740466   0.9555114   1.25276297  0.00000000
## 81      1.704748   0.8754687   1.33500107  0.09531018
## 82      1.704748   0.8754687   1.30833282  0.00000000
## 83      1.757858   0.9932518   1.36097655  0.18232156
## 84      1.791759   0.9932518   1.62924054  0.47000363
## 85      1.686399   1.0986123   1.50407740  0.40546511
## 86      1.791759   1.2237754   1.50407740  0.47000363
## 87      1.902108   1.1314021   1.54756251  0.40546511
## 88      1.840550   0.8329091   1.48160454  0.26236426
## 89      1.722767   1.0986123   1.41098697  0.26236426
## 90      1.704748   0.9162907   1.38629436  0.26236426
## 91      1.704748   0.9555114   1.48160454  0.18232156
## 92      1.808289   1.0986123   1.52605630  0.33647224
## 93      1.757858   0.9555114   1.38629436  0.18232156
## 94      1.609438   0.8329091   1.19392247  0.00000000
## 95      1.722767   0.9932518   1.43508453  0.26236426
## 96      1.740466   1.0986123   1.43508453  0.18232156
## 97      1.740466   1.0647107   1.43508453  0.26236426
## 98      1.824549   1.0647107   1.45861502  0.26236426
## 99      1.629241   0.9162907   1.09861229  0.09531018
## 100     1.740466   1.0296194   1.41098697  0.26236426
## 101     1.840550   1.1939225   1.79175947  0.91629073
## 102     1.757858   0.9932518   1.62924054  0.64185389
## 103     1.960095   1.0986123   1.77495235  0.74193734
## 104     1.840550   1.0647107   1.72276660  0.58778666
## 105     1.871802   1.0986123   1.75785792  0.78845736
## 106     2.028148   1.0986123   1.88706965  0.74193734
## 107     1.589235   0.9162907   1.50407740  0.53062825
## 108     1.987874   1.0647107   1.84054963  0.58778666
## 109     1.902108   0.9162907   1.75785792  0.58778666
## 110     1.974081   1.2809338   1.80828877  0.91629073
## 111     1.871802   1.1631508   1.62924054  0.69314718
## 112     1.856298   0.9932518   1.66770682  0.64185389
## 113     1.916923   1.0986123   1.70474809  0.74193734
## 114     1.740466   0.9162907   1.60943791  0.69314718
## 115     1.757858   1.0296194   1.62924054  0.87546874
## 116     1.856298   1.1631508   1.66770682  0.83290912
## 117     1.871802   1.0986123   1.70474809  0.58778666
## 118     2.041220   1.3350011   1.90210753  0.78845736
## 119     2.041220   0.9555114   1.93152141  0.83290912
## 120     1.791759   0.7884574   1.60943791  0.40546511
## 121     1.931521   1.1631508   1.74046617  0.83290912
## 122     1.722767   1.0296194   1.58923521  0.69314718
## 123     2.041220   1.0296194   1.90210753  0.69314718
## 124     1.840550   0.9932518   1.58923521  0.58778666
## 125     1.902108   1.1939225   1.74046617  0.74193734
## 126     1.974081   1.1631508   1.79175947  0.58778666
## 127     1.824549   1.0296194   1.56861592  0.58778666
## 128     1.808289   1.0986123   1.58923521  0.58778666
## 129     1.856298   1.0296194   1.72276660  0.74193734
## 130     1.974081   1.0986123   1.75785792  0.47000363
## 131     2.001480   1.0296194   1.80828877  0.64185389
## 132     2.066863   1.3350011   1.85629799  0.69314718
## 133     1.856298   1.0296194   1.72276660  0.78845736
## 134     1.840550   1.0296194   1.62924054  0.40546511
## 135     1.808289   0.9555114   1.72276660  0.33647224
## 136     2.041220   1.0986123   1.80828877  0.83290912
## 137     1.840550   1.2237754   1.72276660  0.87546874
## 138     1.856298   1.1314021   1.70474809  0.58778666
## 139     1.791759   1.0986123   1.56861592  0.58778666
## 140     1.931521   1.1314021   1.68639895  0.74193734
## 141     1.902108   1.1314021   1.72276660  0.87546874
## 142     1.931521   1.1314021   1.62924054  0.83290912
## 143     1.757858   0.9932518   1.62924054  0.64185389
## 144     1.916923   1.1631508   1.77495235  0.83290912
## 145     1.902108   1.1939225   1.74046617  0.91629073
## 146     1.902108   1.0986123   1.64865863  0.83290912
## 147     1.840550   0.9162907   1.60943791  0.64185389
## 148     1.871802   1.0986123   1.64865863  0.69314718
## 149     1.824549   1.2237754   1.68639895  0.83290912
## 150     1.774952   1.0986123   1.62924054  0.58778666
iris_log<-cbind(log.ir,ir.species)

log.ir %>% shapiro_test(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
## # A tibble: 4 x 3
##   variable     statistic        p
##   <chr>            <dbl>    <dbl>
## 1 Petal.Length     0.817 2.04e-12
## 2 Petal.Width      0.821 3.05e-12
## 3 Sepal.Length     0.983 5.39e- 2
## 4 Sepal.Width      0.989 3.01e- 1

scaling and mean centering

iris_data<-iris[,1:4]
iris_center_scale_data<-as.data.frame(scale(iris_data, center = TRUE, scale = TRUE))
iris_center_scale<-cbind(iris_center_scale_data, ir.species)
iris_log_center_scale_data<-as.data.frame(scale(log.ir, center = TRUE, scale = TRUE))
iris_log_center_scale<-cbind(iris_log_center_scale_data, ir.species)
##boxspot comparison with ggplot
g.boxplot_iris<-iris %>% dplyr::select(Species, everything()) %>% tidyr::gather("id", "value",2:5) %>% 
  ggplot(., aes(x = id, y = value))+geom_boxplot()

g.boxplot_iris_log<-iris_log %>% dplyr::select(ir.species, everything()) %>% tidyr::gather("id", "value",2:5) %>% 
  ggplot(., aes(x = id, y = value))+geom_boxplot()

g.boxplot_iris_center_scale<-iris_center_scale %>% dplyr::select(ir.species, everything()) %>% tidyr::gather("id", "value",2:5) %>% 
  ggplot(., aes(x = id, y = value))+geom_boxplot()

g.boxplot_iris_log_center_scale<-iris_log_center_scale %>% dplyr::select(ir.species, everything()) %>% tidyr::gather("id", "value",2:5) %>% 
  ggplot(., aes(x = id, y = value))+geom_boxplot()

g.boxplot_iris_comparison<-plot_grid(g.boxplot_iris, g.boxplot_iris_log, g.boxplot_iris_center_scale, g.boxplot_iris_log_center_scale, ncol=2,  align = 'v', rel_heights = c(1/5, 1/5, 1/5, 1/5),
                           labels = c('A', 'B', 'C', 'D'))
g.boxplot_iris_comparison

# A. g.boxplot_iris, 
# B. g.boxplot_iris_log,
# C. g.boxplot_iris_center_scale,
# D. g.boxplot_iris_log_center_scale
##PCA dengan ggplot profil lengkap
g.pca_iris_compl<-autoplot(prcomp(iris_data), data = iris, colour = 'Species', frame = T, loadings = TRUE, loadings.label = TRUE)
g.pca_iris_compl

g.scree_iris<-plot(prcomp(iris_data))

g.scree_iris
## NULL
##PCA dengan ggplot perbandingan
g.pca_iris<-autoplot(prcomp(iris_data), data = iris, colour = 'Species', frame = T)
g.pca_iris_log<-autoplot(prcomp(log.ir), data = iris_log, colour = 'ir.species', frame = T)
g.pca_iris_center_scale<-autoplot(prcomp(iris_center_scale_data, center =  FALSE), data = iris_center_scale, colour = 'ir.species', frame = T)
g.pca_iris_log_center_scale<-autoplot(prcomp(iris_log_center_scale_data, center =  FALSE), data = iris_log_center_scale, colour = 'ir.species', frame = T)

g.pca_iris_comparison<-plot_grid(g.pca_iris, g.pca_iris_log, g.pca_iris_center_scale, g.pca_iris_log_center_scale, ncol=2,  align = 'v', rel_heights = c(1/5, 1/5, 1/5, 1/5),
                           labels = c('A', 'B', 'C', 'D'))
g.pca_iris_comparison

# A. g.pca_iris, 
# B. g.pca_iris_log,
# C. g.pca_iris_center_scale,
# D. g.pca_iris_log_center_scale

No comments:

Post a Comment