library(readxl) 

ex.data <- read_xlsx("G:/DATA/strofadia_Jphoenicea_trees_v2.xlsx")

dat <- data.frame(ex.data)
dat
Age <- dat$Age
H <- dat$Heightcm
Dcm <- dat$Diamcm
Rcm <- dat$Radiuscm

dat3 <- data.frame(Age, H, Dcm, Rcm)

Correlation test με την base R

εντολή cor δίνει μόνο το correlation, αλλά μπορεί να γίνει και σε πίνακα

εντολή cor.test που δίνει και πληροφορία για το p-value, αλλά μόνο ανά δύο

cor(dat3)
##           Age         H       Dcm       Rcm
## Age 1.0000000 0.8193401 0.4711418 0.9087815
## H   0.8193401 1.0000000 0.3280815 0.8470346
## Dcm 0.4711418 0.3280815 1.0000000 0.4592861
## Rcm 0.9087815 0.8470346 0.4592861 1.0000000
cor.test(Age,H)
## 
##  Pearson's product-moment correlation
## 
## data:  Age and H
## t = 7.2872, df = 26, p-value = 9.718e-08
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.6427312 0.9132558
## sample estimates:
##       cor 
## 0.8193401

με τη βιβλιοθήκη Hmisc και την εντολή rcorr μπορούμε να έχουμε

και σε μορφή πίνακα τόσο τα correlation όσο και τα p-value

χρήσιμο για το corrplot

library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
table1 <- rcorr(as.matrix(dat3))
table1
##      Age    H  Dcm  Rcm
## Age 1.00 0.82 0.47 0.91
## H   0.82 1.00 0.33 0.85
## Dcm 0.47 0.33 1.00 0.46
## Rcm 0.91 0.85 0.46 1.00
## 
## n= 28 
## 
## 
## P
##     Age    H      Dcm    Rcm   
## Age        0.0000 0.0114 0.0000
## H   0.0000        0.0883 0.0000
## Dcm 0.0114 0.0883        0.0139
## Rcm 0.0000 0.0000 0.0139

ΕΞΑΓΩΓΗ ΤΩΝ ΑΠΟΤΕΛΕΣΜΑΤΩΝ σε μορφή Excel

# Δημιουργία αντικειμένων από το προηγούμενο correlation (μαζί με τα p-values)
v1 <- table1$r
v2 <- table1$P

# ενοποίηση σε data frame
result <- data.frame(v1,v2)

# φόρτωση κατάλληλης βιβλιοθήκης
library(writexl)

# εγγραφή του αρχείου σε δίσκο
write_xlsx(result, "G:/result.xlsx", col_names=TRUE)
ΔΗΜΙΟΥΡΓΙΑ ΔΙΑΓΡΑΜΜΑΤΩΝ
# διάγραμμα scatter plot με γραμμή προσαρμογής

# φόρτωση βιβλιοθήκης
library (ggpubr)
## Loading required package: ggplot2
ggscatter(dat3, x="Age", y="Dcm", 
          add ="reg.line",conf.int = TRUE,
          cor.coef = TRUE,
          cor.method = "pearson")

ggscatter(dat3, x="Age", y="H", 
          add ="reg.line",conf.int = TRUE,
          cor.coef = TRUE,
          cor.method = "pearson")

ggscatter(dat3, x="Age", y="H",
          cor.coef = TRUE,
          cor.method = "pearson",
          add ="loess",conf.int = TRUE,
          xlab= "Ηλικία δέντρων", ylab="Ύψος δέντρων")

### ΑΝΑΓΝΩΡΙΣΗ OUTLIERS

# φόρτωση βιβλιοθήκης

library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
## 
##     filter
# προσδιορισμός outlier
identify_outliers(
  data = dat3,
  variable = "H"
)
# προσδιορισμός της τιμής
out <- boxplot.stats(dat3$H)$out
out
## [1] 25
# προσδιορισμός στη σειρά του πίνακα
out_ind <- which(dat3$H %in% c(out))
out_ind
## [1] 10
# διάγραμμα boxplot με το outlier
boxplot(dat3$H)
mtext(paste("Outliers: ", paste(out, collapse = ", "))) # εγγραφή της τιμης

CORRPLOT

# φόρτωση βιβλιοθήκης
library(corrplot)
## corrplot 0.94 loaded
# εφαρμογή correlation (όπως πριν) και δημιουργία αντικειμένου για το corrplot 

plot <- cor(dat3)

## εφαρμογή διαφορετικών Plots
corrplot(plot)

corrplot(plot, method = "circle")

corrplot(plot, method = "square")

corrplot(plot, method = 'number') 

corrplot(plot, method = "ellipse") 

corrplot(plot, method = 'color', order = 'alphabet')

corrplot(plot, order = 'AOE') # after 'AOE' reorder

corrplot(plot, method = 'shade', order = 'AOE', diag =FALSE)

corrplot(plot, type = 'lower', diag = FALSE)

corrplot(plot, order = 'FPC', type = 'lower', diag = FALSE)

corrplot.mixed(plot, order = 'AOE')

corrplot.mixed(plot, lower = 'ellipse', upper = 'pie', order = 'hclust')

corrplot(plot, order = 'AOE', addCoef.col = 'red')

## Δημιουργία plot με αστεράκια p-value πάνω σε αυτά

# επανάληψη (αυτό είχε γίνει και πριν) για να δημιουργηθεί ο πίνακας με τα p-value
library(Hmisc)
table1 <- rcorr(as.matrix(dat3))
table1
##      Age    H  Dcm  Rcm
## Age 1.00 0.82 0.47 0.91
## H   0.82 1.00 0.33 0.85
## Dcm 0.47 0.33 1.00 0.46
## Rcm 0.91 0.85 0.46 1.00
## 
## n= 28 
## 
## 
## P
##     Age    H      Dcm    Rcm   
## Age        0.0000 0.0114 0.0000
## H   0.0000        0.0883 0.0000
## Dcm 0.0114 0.0883        0.0139
## Rcm 0.0000 0.0000 0.0139
# στο P.mat φορτώνουμε την παράμετρο με τα p-value
corrplot(plot, p.mat = table1$P, method = 'circle', diag = FALSE, type = 'upper',
         sig.level = c(0.001, 0.01, 0.05), pch.cex = 2,
         insig = 'label_sig', pch.col = 'grey20', order = 'AOE')

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Including Plots