setwd("~/Google Drive/Nuevo_perro/data/random")
datos<-read.table("random_AM.csv", header=T, sep=',')
attach(datos)
# Steps for a randomization test on a correlation coefficient (sensibilities vs. trait means)
# Root:Shoot (raw) sensibilities and means
rs.plas <- ((rs.c-rs.d)/((mean(rs.c)-mean(rs.d))))
rs.avg <- ((rs.c+rs.d)/2)
rs.obt <- cor(rs.plas,rs.avg)
cat("The obtained correlation is ",rs.obt,'\n')
## The obtained correlation is 0.5029471
# Randomizing -using the permutation of- the treatment group
perm=1000
r.random <- matrix(0, nrow = length(rs.c), ncol=perm)
set.seed(1086)
for (i in 1:perm) {
X <- sample(rs.d, 36, replace = FALSE)
r.random [,i] <- X
}
# Estimating plasticities (as sensibilities) from the permutation matrix and the control group
r.randp <- matrix(0, nrow = length(rs.c), ncol=perm)
for (i in 1:perm) {
Y <- rs.c
Z <- r.random[,i]
r.randp [,i] <- ((Y-Z)/((mean(Y)-mean(Z))))
}
# Estimating means over both environments (the permutation matrix and the control group)
r.randmean <- matrix(0, nrow = length(rs.c), ncol=perm)
for (i in 1:perm) {
r.randmean[,i] <- ((Y+Z)/2)
}
# Finding correlations between mean over both environments and sensibilities
permf=1000
r.randcor <- numeric(permf)
for (i in 1:permf) {
P <- r.randp[,i]
M <- r.randmean[,i]
r.randcor[i] <- cor(P,M)
}
Z <- mean(r.randcor)
Z
## [1] 0.6379384
quantile(r.randcor, probs = seq(0, 1, 0.05))
## 0% 5% 10% 15% 20% 25% 30% 35%
## 0.2192269 0.5166765 0.5415049 0.5631952 0.5804918 0.5922382 0.6049229 0.6153600
## 40% 45% 50% 55% 60% 65% 70% 75%
## 0.6254100 0.6346524 0.6423210 0.6523553 0.6627878 0.6714933 0.6785990 0.6868641
## 80% 85% 90% 95% 100%
## 0.6964846 0.7084936 0.7237794 0.7417829 0.8084779
# Testing if the randomized correlation is less than or equal to the obtained correlation
prob <- length(r.randcor[r.randcor <= rs.obt])/permf
cat("Probability randomized r <= r.obt",prob)
## Probability randomized r <= r.obt 0.033
# Histogram for the distribution of randomized samples of r
hist(r.randcor, breaks = 50, main = expression(paste("Distribution around Spurious ",rho, " = 0.64")), xlab = "r from randomized samples")
rs.obt <- round(rs.obt, digits = 2)
legend(0.45, 48, rs.obt, bty = "n")
arrows(0.5,40,0.5, 0.1)
# Plotting the correlation between (raw) sensibilities and means
res.rs <- cor.test(rs.plas,rs.avg,
method = "pearson")
res.rs
##
## Pearson's product-moment correlation
##
## data: rs.plas and rs.avg
## t = 3.393, df = 34, p-value = 0.00177
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.2089350 0.7135746
## sample estimates:
## cor
## 0.5029471
corre.rs <- data.frame(rs.plas,rs.avg)
library("ggpubr")
## Loading required package: ggplot2

ggscatter(corre.rs, x = "rs.plas", y = "rs.avg",
add = "reg.line", conf.int = F,
cor.coef = TRUE, cor.method = "pearson",
xlab = "Plasticity in ontogenetic root:shoot decrease", ylab = "Mean ontogenetic root:shoot decrease")
## `geom_smooth()` using formula 'y ~ x'

# Leaf Weight Ratio (lwr), raw sensibilities and means
lwr.plas <- ((lwr.c-lwr.d)/((mean(lwr.c)-mean(lwr.d))))
lwr.avg <- ((lwr.c+lwr.d)/2)
lwr.obt <- cor(lwr.plas,lwr.avg)
cat("The obtained correlation is ",lwr.obt,'\n')
## The obtained correlation is -0.4358278
# Randomizing -using the permutation of- the treatment group
perm=1000
r.random <- matrix(0, nrow = length(lwr.c), ncol=perm)
set.seed(1086)
for (i in 1:perm) {
X <- sample(lwr.d, 36, replace = FALSE)
r.random [,i] <- X
}
# Estimating plasticities (as sensibilities) from the permutation matrix and the control group
r.randp <- matrix(0, nrow = length(lwr.c), ncol=perm)
for (i in 1:perm) {
Y <- lwr.c
Z <- r.random[,i]
r.randp [,i] <- ((Y-Z)/((mean(Y)-mean(Z))))
}
# Estimating means over both environments (the permutation matrix and the control group)
r.randmean <- matrix(0, nrow = length(lwr.c), ncol=perm)
for (i in 1:perm) {
r.randmean[,i] <- ((Y+Z)/2)
}
# Finding correlations between mean over both environments and sensibilities
permf=1000
r.randcor <- numeric(permf)
for (i in 1:permf) {
P <- r.randp[,i]
M <- r.randmean[,i]
r.randcor[i] <- cor(P,M)
}
Z <- mean(r.randcor)
Z
## [1] 0.3421512
quantile(r.randcor, probs = seq(0, 1, 0.05))
## 0% 5% 10% 15% 20% 25% 30%
## -0.4018878 0.1246621 0.1746109 0.2103745 0.2313135 0.2538294 0.2748432
## 35% 40% 45% 50% 55% 60% 65%
## 0.2947322 0.3149300 0.3305729 0.3469921 0.3621089 0.3773416 0.3928943
## 70% 75% 80% 85% 90% 95% 100%
## 0.4167667 0.4382010 0.4588324 0.4812430 0.5102128 0.5469898 0.6921987
# Testing if the randomized correlation is less than or equal to the obtained correlation
prob <- length(r.randcor[r.randcor <= lwr.obt])/permf
cat("Probability randomized r <= r.obt",prob)
## Probability randomized r <= r.obt 0
# Histogram for the distribution of randomized samples of r
hist(r.randcor, breaks = 50, xlim = c(-0.6, 0.8), main = expression(paste("Distribution around Spurious ",rho, " = 0.34")), xlab = "r from randomized samples")
lwr.obt <- round(lwr.obt, digits = 2)
legend(-0.56, 48, lwr.obt, bty = "n")
arrows(-0.44,40,-0.44, 0.1)

# Plotting the correlation between (raw) sensibilities and means
res.lwr <- cor.test(lwr.plas,lwr.avg,
method = "pearson")
res.lwr
##
## Pearson's product-moment correlation
##
## data: lwr.plas and lwr.avg
## t = -2.8236, df = 34, p-value = 0.007884
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.6686263 -0.1252220
## sample estimates:
## cor
## -0.4358278
corre.lwr <- data.frame(lwr.plas,lwr.avg)
ggscatter(corre.lwr, x = "lwr.plas", y = "lwr.avg",
add = "reg.line", conf.int = F,
cor.coef = TRUE, cor.method = "pearson",
xlab = "Plasticity in ontogenetic LWR decrease", ylab = "Mean ontogenetic LWR decrease")
## `geom_smooth()` using formula 'y ~ x'

# Leaf Area Ratio (lar), raw sensibilities and means
lar.plas <- ((lar.c-lar.d)/((mean(lar.c)-mean(lar.d))))
lar.avg <- ((lar.c+lar.d)/2)
lar.obt <- cor(lar.plas,lar.avg)
cat("The obtained correlation is ",lar.obt,'\n')
## The obtained correlation is 0.2062875
# Randomizing -using the permutation of- the treatment group
perm=1000
r.random <- matrix(0, nrow = length(lar.c), ncol=perm)
set.seed(1086)
for (i in 1:perm) {
X <- sample(lar.d, 36, replace = FALSE)
r.random [,i] <- X
}
# Estimating plasticities (as sensibilities) from the permutation matrix and the control group
r.randp <- matrix(0, nrow = length(lar.c), ncol=perm)
for (i in 1:perm) {
Y <- lar.c
Z <- r.random[,i]
r.randp [,i] <- ((Y-Z)/((mean(Y)-mean(Z))))
}
# Estimating means over both environments (the permutation matrix and the control group)
r.randmean <- matrix(0, nrow = length(lar.c), ncol=perm)
for (i in 1:perm) {
r.randmean[,i] <- ((Y+Z)/2)
}
# Finding correlations between mean over both environments and sensibilities
permf=1000
r.randcor <- numeric(permf)
for (i in 1:permf) {
P <- r.randp[,i]
M <- r.randmean[,i]
r.randcor[i] <- cor(P,M)
}
Z <- mean(r.randcor)
Z
## [1] 0.5746609
quantile(r.randcor, probs = seq(0, 1, 0.05))
## 0% 5% 10% 15% 20% 25% 30% 35%
## 0.1383511 0.4474760 0.4727286 0.4891303 0.5055705 0.5209035 0.5358128 0.5457008
## 40% 45% 50% 55% 60% 65% 70% 75%
## 0.5567508 0.5683598 0.5780602 0.5863020 0.5953192 0.6045367 0.6155328 0.6264186
## 80% 85% 90% 95% 100%
## 0.6402756 0.6560774 0.6766075 0.7057404 0.7898186
# Testing if the randomized correlation is less than or equal to the obtained correlation
prob <- length(r.randcor[r.randcor <= lar.obt])/permf
cat("Probability randomized r <= r.obt",prob)
## Probability randomized r <= r.obt 0.001
# Histogram for the distribution of randomized samples of r
hist(r.randcor, breaks = 50, main = expression(paste("Distribution around Spurious ",rho, " = 0.57")), xlab = "r from randomized samples")
lar.obt <- round(lar.obt, digits = 2)
legend(0.16, 48, lar.obt, bty = "n")
arrows(0.21,40,0.21, 0.1)

res.lar <- cor.test(lar.plas,lar.avg,
method = "pearson")
res.lar
##
## Pearson's product-moment correlation
##
## data: lar.plas and lar.avg
## t = 1.2293, df = 34, p-value = 0.2274
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.1311357 0.5008774
## sample estimates:
## cor
## 0.2062875
corre.lar <- data.frame(lar.plas,lar.avg)
library("ggpubr")
ggscatter(corre.lar, x = "lar.plas", y = "lar.avg",
conf.int = F,
cor.coef = TRUE, cor.method = "pearson",
xlab = "Plasticity in ontogenetic LAR decrease", ylab = "Mean ontogenetic LAR decrease")

# Net Assimilation Rate (nar), raw sensibilities and means
nar.plas <- ((nar.c-nar.d)/((mean(nar.c)-mean(nar.d))))
nar.avg <- ((nar.c+nar.d)/2)
nar.obt <- cor(nar.plas,nar.avg)
cat("The obtained correlation is ",nar.obt,'\n')
## The obtained correlation is 0.1605884
# Randomizing -using the permutation of- the treatment group
perm=1000
r.random <- matrix(0, nrow = length(nar.c), ncol=perm)
set.seed(1086)
for (i in 1:perm) {
X <- sample(nar.d, 36, replace = FALSE)
r.random [,i] <- X
}
# Estimating plasticities (as sensibilities) from the permutation matrix and the control group
r.randp <- matrix(0, nrow = length(nar.c), ncol=perm)
for (i in 1:perm) {
Y <- nar.c
Z <- r.random[,i]
r.randp [,i] <- ((Y-Z)/((mean(Y)-mean(Z))))
}
# Estimating means over both environments (the permutation matrix and the control group)
r.randmean <- matrix(0, nrow = length(nar.c), ncol=perm)
for (i in 1:perm) {
r.randmean[,i] <- ((Y+Z)/2)
}
# Finding correlations between mean over both environments and sensibilities
permf=1000
r.randcor <- numeric(permf)
for (i in 1:permf) {
P <- r.randp[,i]
M <- r.randmean[,i]
r.randcor[i] <- cor(P,M)
}
Z <- mean(r.randcor)
Z
## [1] 0.6488874
quantile(r.randcor, probs = seq(0, 1, 0.05))
## 0% 5% 10% 15% 20% 25% 30% 35%
## 0.1164689 0.5179307 0.5512046 0.5720977 0.5908203 0.6052994 0.6155784 0.6270255
## 40% 45% 50% 55% 60% 65% 70% 75%
## 0.6363147 0.6458055 0.6531914 0.6637868 0.6745680 0.6820203 0.6938526 0.7035764
## 80% 85% 90% 95% 100%
## 0.7115977 0.7232636 0.7386877 0.7594847 0.8345932
# Testing if the randomized correlation is less than or equal to the obtained correlation
prob <- length(r.randcor[r.randcor <= nar.obt])/permf
cat("Probability randomized r <= r.obt",prob)
## Probability randomized r <= r.obt 0.001
# Histogram for the distribution of randomized samples of r
hist(r.randcor, breaks = 50, main = expression(paste("Distribution around Spurious ",rho, " = 0.65")), xlab = "r from randomized samples")
nar.obt <- round(nar.obt, digits = 2)
legend(0.1, 52, nar.obt, bty = "n")
arrows(0.16,40,0.16, 0.1)

res.nar <- cor.test(nar.plas,nar.avg,
method = "pearson")
res.nar
##
## Pearson's product-moment correlation
##
## data: nar.plas and nar.avg
## t = 0.9487, df = 34, p-value = 0.3495
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.1773016 0.4646117
## sample estimates:
## cor
## 0.1605884
corre.nar <- data.frame(nar.plas,nar.avg)
library("ggpubr")
ggscatter(corre.nar, x = "nar.plas", y = "nar.avg",
conf.int = F,
cor.coef = TRUE, cor.method = "pearson",
xlab = "Plasticity in ontogenetic NAR decrease", ylab = "Mean ontogenetic NAR decrease")

# Specific Leaf Area (sla), raw sensibilities and means
sla.plas <- ((sla.c-sla.d)/((mean(sla.c)-mean(sla.d))))
sla.avg <- ((sla.c+sla.d)/2)
sla.obt <- cor(sla.plas,sla.avg)
cat("The obtained correlation is ",sla.obt,'\n')
## The obtained correlation is 0.1049691
# Randomizing -using the permutation of- the treatment group
perm=1000
r.random <- matrix(0, nrow = length(sla.c), ncol=perm)
set.seed(1086)
for (i in 1:perm) {
X <- sample(sla.d, 36, replace = FALSE)
r.random [,i] <- X
}
# Estimating plasticities (as sensibilities) from the permutation matrix and the control group
r.randp <- matrix(0, nrow = length(sla.c), ncol=perm)
for (i in 1:perm) {
Y <- sla.c
Z <- r.random[,i]
r.randp [,i] <- ((Y-Z)/((mean(Y)-mean(Z))))
}
# Estimating means over both environments (the permutation matrix and the control group)
r.randmean <- matrix(0, nrow = length(sla.c), ncol=perm)
for (i in 1:perm) {
r.randmean[,i] <- ((Y+Z)/2)
}
# Finding correlations between mean over both environments and sensibilities
permf=1000
r.randcor <- numeric(permf)
for (i in 1:permf) {
P <- r.randp[,i]
M <- r.randmean[,i]
r.randcor[i] <- cor(P,M)
}
Z <- mean(r.randcor)
Z
## [1] 0.5438619
quantile(r.randcor, probs = seq(0, 1, 0.05))
## 0% 5% 10% 15% 20% 25% 30%
## 0.09098452 0.38805151 0.42469983 0.44951179 0.47158306 0.49078687 0.50384851
## 35% 40% 45% 50% 55% 60% 65%
## 0.51581041 0.52743698 0.53694976 0.54971877 0.56115145 0.57292397 0.58350760
## 70% 75% 80% 85% 90% 95% 100%
## 0.59366396 0.60749777 0.61844446 0.62938826 0.65002875 0.67330164 0.76288412
# Testing if the randomized correlation is less than or equal to the obtained correlation
prob <- length(r.randcor[r.randcor <= sla.obt])/permf
cat("Probability randomized r <= r.obt",prob)
## Probability randomized r <= r.obt 0.001
# Histogram for the distribution of randomized samples of r
hist(r.randcor, breaks = 50, main = expression(paste("Distribution around Spurious ",rho, " = 0.54")), xlab = "r from randomized samples")
sla.obt <- round(sla.obt, digits = 2)
legend(0.05, 45, sla.obt, bty = "n")
arrows(0.1,39,0.1, 0.1)

res.sla <- cor.test(sla.plas,sla.avg,
method = "pearson")
res.sla
##
## Pearson's product-moment correlation
##
## data: sla.plas and sla.avg
## t = 0.61547, df = 34, p-value = 0.5423
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.2315519 0.4190534
## sample estimates:
## cor
## 0.1049691
corre.sla <- data.frame(sla.plas,sla.avg)
library("ggpubr")
ggscatter(corre.sla, x = "sla.plas", y = "sla.avg",
conf.int = F,
cor.coef = TRUE, cor.method = "pearson",
xlab = "Plasticity in ontogenetic SLA decrease", ylab = "Mean ontogenetic SLA decrease")

# Relative Growth Rate (rgr), raw sensibilities and means
rgr.plas <- ((rgr.c-rgr.d)/((mean(rgr.c)-mean(rgr.d))))
rgr.avg <- ((rgr.c+rgr.d)/2)
rgr.obt <- cor(rgr.plas,rgr.avg)
cat("The obtained correlation is ",rgr.obt,'\n')
## The obtained correlation is 0.4670133
# Randomizing -using the permutation of- the treatment group
perm=1000
r.random <- matrix(0, nrow = length(rgr.c), ncol=perm)
set.seed(1086)
for (i in 1:perm) {
X <- sample(rgr.d, 36, replace = FALSE)
r.random [,i] <- X
}
# Estimating plasticities (as sensibilities) from the permutation matrix and the control group
r.randp <- matrix(0, nrow = length(rgr.c), ncol=perm)
for (i in 1:perm) {
Y <- rgr.c
Z <- r.random[,i]
r.randp [,i] <- ((Y-Z)/((mean(Y)-mean(Z))))
}
# Estimating means over both environments (the permutation matrix and the control group)
r.randmean <- matrix(0, nrow = length(rgr.c), ncol=perm)
for (i in 1:perm) {
r.randmean[,i] <- ((Y+Z)/2)
}
# Finding correlations between mean over both environments and sensibilities
permf=1000
r.randcor <- numeric(permf)
for (i in 1:permf) {
P <- r.randp[,i]
M <- r.randmean[,i]
r.randcor[i] <- cor(P,M)
}
Z <- mean(r.randcor)
Z
## [1] 0.6597969
quantile(r.randcor, probs = seq(0, 1, 0.05))
## 0% 5% 10% 15% 20% 25% 30% 35%
## 0.3107825 0.5564764 0.5793311 0.5935034 0.6070897 0.6198858 0.6297593 0.6364184
## 40% 45% 50% 55% 60% 65% 70% 75%
## 0.6433396 0.6504781 0.6596137 0.6686391 0.6767552 0.6853488 0.6954501 0.7045066
## 80% 85% 90% 95% 100%
## 0.7150647 0.7251862 0.7415245 0.7592885 0.8504128
# Testing if the randomized correlation is less than or equal to the obtained correlation
prob <- length(r.randcor[r.randcor <= rgr.obt])/permf
cat("Probability randomized r <= r.obt",prob)
## Probability randomized r <= r.obt 0.002
# Histogram for the distribution of randomized samples of r
hist(r.randcor, breaks = 50, main = expression(paste("Distribution around Spurious ",rho, " = 0.66")), xlab = "r from randomized samples")
rgr.obt <- round(rgr.obt, digits = 2)
legend(0.43, 48, rgr.obt, bty = "n")
arrows(0.47,39,0.47, 0.1)

res.rgr <- cor.test(rgr.plas,rgr.avg,
method = "pearson")
res.rgr
##
## Pearson's product-moment correlation
##
## data: rgr.plas and rgr.avg
## t = 3.0796, df = 34, p-value = 0.004085
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.1635749 0.6897242
## sample estimates:
## cor
## 0.4670133
corre.rgr <- data.frame(rgr.plas,rgr.avg)
library("ggpubr")
ggscatter(corre.rgr, x = "rgr.plas", y = "rgr.avg",
add = "reg.line", conf.int = F,
cor.coef = TRUE, cor.method = "pearson",
xlab = "Plasticity in ontogenetic RGR decrease", ylab = "Mean ontogenetic RGR decrease")
## `geom_smooth()` using formula 'y ~ x'
