Description Usage Arguments Value Author(s) References Examples
'Pst' calculates Pst values of the quantitative measures considered and also their confidence intervals.
1 |
data |
a dataframe with as many rows as individuals. The first column contains the name of the population to which the individual belongs, the others contain quantitative variables. |
ci |
if ci=1 the confidence intervals are added to Pst values. |
csh |
the value of c/h^2, where c is the assumed additive genetic proportion of differences between populations and where h^2 is (narrow-sense heritability) the assumed additive genetic proportion of differences between individuals within populations. |
va |
a vector containing the selected variables names or numbers (i.e. those of the quantitative measures considered). If va=0 all the variables are selected. |
boot |
the number of data frames generated to determine the confidence interval with the bootstrap method. |
Pw |
a vector containing the names of the two populations considered to obtain pairwise Pst. |
Rp |
a vector containing the names of the populations to be deleted. |
Ri |
a vector containing each number of individual to be deleted. The vector Ri must contain existent individuals, each of them once. |
pe |
the confidence level of the calculated interval. |
The sizes of each population considered. Pst values of the selected populations (for quantitative traits considered) and if ci=1 their confidence intervals.
Blondeau Da Silva Stephane - Da Silva Anne.
Spitze K., 1993. Population structure in Daphnia obtusa: Quantitative genetic and allozymic variation. Genetics 135: 367-374.
Leinonen T., Cano J.M., Makinen H., Merila J., 2006. Contrasting patterns of body shape and neutral genetic divergence in marine and lake populations of threespine sticklebacks. Journal of Evolutionary Biology 19: 1803-1812.
Brommer J.E., 2011. Whither Pst? The approximation of Qst by Pst in evolutionary and conservation biology. Journal Evolution Biology 24: 1160-1168.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | data(test)
Pst(test)
# Pst(test,csh=0.2,ci=1)
Pst(test,va="QM2",ci=1,Rp="D",boot=50)
# Pst(test,va=c(5,8:11),ci=1,boot=2000,Ri=56,Rp="A",pe=0.9)
# Pst(test,ci=1,Ri=c(7,55:59),Pw=c("A","D"))
## The function is currently defined as
function (data, ci = 0, csh = 1, va = 0, boot = 1000, Pw = 0,
Rp = 0, Ri = 0, pe = 0.95)
{
nonNa.clm <- function(data, clm) {
nb.ind = dim(data)[1]
nb.na = 0
for (i in 1:nb.ind) if (is.na(data[i, clm]))
nb.na = nb.na + 1
return(nb.ind - nb.na)
}
dat.fra.prep <- function(data) {
nb.var = dim(data)[2] - 1
data = as.data.frame(data)
data[, 1] = as.character(data[, 1])
for (i in 1:nb.var) {
if (is.numeric(data[, i + 1]) == FALSE)
data[, i + 1] = as.numeric(as.character(data[,
i + 1]))
}
dat.sta <- function(dat) {
nb.vari = dim(dat)[2] - 1
st.dev = rep(0, nb.vari)
mea = rep(0, nb.vari)
for (i in 1:nb.vari) {
nna.clm = nonNa.clm(dat, i + 1)
st.dev[i] = sqrt((nna.clm - 1)/nna.clm) * sd(dat[,
i + 1], na.rm = TRUE)
mea[i] = mean(dat[, i + 1], na.rm = TRUE)
}
for (j in 1:nb.vari) dat[, j + 1] = (dat[, j + 1] -
mea[j])/st.dev[j]
return(dat)
}
data = dat.sta(data)
return(data)
}
dat.rem.ind.pop <- function(data, ind = 0, pop = 0) {
data = as.data.frame(data)
dat.rem.ind <- function(dat, ind) {
nb.rem.ind = length(ind)
nb.ind = dim(dat)[1]
for (i in 1:nb.rem.ind) dat = dat[row.names(dat)[1:(nb.ind -
i + 1)] != ind[i], ]
return(dat)
}
dat.rem.pop <- function(dat, pop) {
nb.rem.pop = length(pop)
for (i in 1:nb.rem.pop) dat = dat[dat[, 1] != pop[i],
]
return(dat)
}
if (ind[1] != 0)
data = dat.rem.ind(data, ind)
if (pop[1] != 0)
data = dat.rem.pop(data, pop)
return(data)
}
dat.pw <- function(data, pw = 0) {
if (pw[1] == 0)
return(data)
else {
data = data[data[, 1] == pw[1] | data[, 1] == pw[2],
]
return(data)
}
}
nb.pop <- function(data) {
data = data[order(data[, 1]), ]
nb.ind = dim(data)[1]
nb.pop = 1
for (i in 1:(nb.ind - 1)) if (data[i, 1] != data[i +
1, 1])
nb.pop = nb.pop + 1
return(nb.pop)
}
pop.freq <- function(data) {
data = data[order(data[, 1]), ]
nb.ind = dim(data)[1]
dat.fra = as.data.frame(data)
nb.pop = 1
for (i in 1:(nb.ind - 1)) if (data[i, 1] != data[i +
1, 1])
nb.pop = nb.pop + 1
pop.freq.vec = rep(1, nb.pop)
name = rep(0, nb.pop)
k = 1
name[1] = as.character(dat.fra[1, 1])
for (i in 2:nb.ind) if (dat.fra[i - 1, 1] == dat.fra[i,
1])
pop.freq.vec[k] = pop.freq.vec[k] + 1
else {
k = k + 1
name[k] = as.character(dat.fra[i, 1])
}
names(pop.freq.vec) = name
return(pop.freq.vec)
}
Pst.val <- function(data, csh = 1) {
nbpop = nb.pop(data)
nb.var = dim(data)[2] - 1
data = data[order(data[, 1]), ]
if (nbpop == 1)
return(rep(0, nb.var))
else {
pop.freq = pop.freq(data)
Pst.clm <- function(dat, clm) {
mea = mean(dat[, clm], na.rm = TRUE)
nna.clm = nonNa.clm(dat, clm)
SSTotal = (nna.clm - 1) * var(dat[, clm], na.rm = TRUE)
mea.pop = rep(0, nbpop)
nna.pop.freq = rep(0, nbpop)
nna.pop.freq[1] = nonNa.clm(dat[1:(pop.freq[1]),
], clm)
nb.allna.pop = 0
if (nna.pop.freq[1] == 0)
nb.allna.pop = 1
else mea.pop[1] = mean(dat[1:(pop.freq[1]), clm],
na.rm = TRUE)
for (i in 2:nbpop) {
nna.pop.freq[i] = nonNa.clm(dat[(sum(pop.freq[1:(i -
1)]) + 1):(sum(pop.freq[1:i])), ], clm)
if (nna.pop.freq[i] != 0)
mea.pop[i] = mean(dat[(sum(pop.freq[1:(i -
1)]) + 1):(sum(pop.freq[1:i])), clm], na.rm = TRUE)
else nb.allna.pop = nb.allna.pop + 1
}
SSBetween = sum(nna.pop.freq * (mea.pop - mea)^2)
SSWithin = SSTotal - SSBetween
if ((nna.clm - nbpop + nb.allna.pop) * (nbpop -
nb.allna.pop - 1) != 0) {
MSWithin = SSWithin/(nna.clm - nbpop + nb.allna.pop)
MSBetween = SSBetween/(nbpop - nb.allna.pop -
1)
return(csh * MSBetween/(csh * MSBetween + 2 *
MSWithin))
}
else {
if ((nna.clm - nbpop + nb.allna.pop) == 0)
return(1)
else return(0)
}
}
pst.val = rep(0, nb.var)
for (j in 1:nb.var) pst.val[j] = Pst.clm(data, j +
1)
return(pst.val)
}
}
boot.pst.va <- function(data, csh, boot, clm) {
nb.ind = dim(data)[1]
dat = data[, c(1, clm)]
boot.val = rep(0, boot)
for (i in 1:boot) {
da = dat[sample(1:nb.ind, nb.ind, T), ]
boot.val[i] = Pst.val(da, csh)
}
return(boot.val)
}
ConInt.pst.va <- function(data, csh, boot, clm, per) {
boot.pst.val = boot.pst.va(data = data, csh = csh, boot = boot,
clm = clm)
boot.pst.val = sort(boot.pst.val)
return(c(boot.pst.val[floor(boot * (1 - per)/2 + 1)],
boot.pst.val[ceiling(boot * (per + 1)/2)]))
}
if (va[1] == 0) {
nb.var = dim(data)[2] - 1
va = 1:nb.var
}
else {
nb.var = length(va)
for (i in 1:nb.var) {
for (j in 2:dim(data)[2]) {
if (names(data)[j] == va[i])
va[i] = j - 1
}
}
va = as.numeric(va)
if (is.na(sum(va)) == TRUE)
return("va is not valid!")
}
data = dat.fra.prep(data)
data = dat.rem.ind.pop(data, ind = Ri, pop = Rp)
data = dat.pw(data, Pw)
print("Populations sizes are:")
print(pop.freq(data))
if (ci != 1) {
output = data.frame(Quant_Varia = names(data)[va + 1],
Pst_Values = Pst.val(data, csh = csh)[va], row.names = NULL)
return(output)
}
if (ci == 1) {
low.bnd = rep(0, nb.var)
up.bnd = rep(0, nb.var)
for (i in 1:nb.var) {
ci.pst.va = ConInt.pst.va(data, csh = csh, boot = boot,
clm = va[i] + 1, per = pe)
low.bnd[i] = ci.pst.va[1]
up.bnd[i] = ci.pst.va[2]
}
output = data.frame(Quant_Varia = names(data)[va + 1],
Pst_Values = Pst.val(data, csh = csh)[va], LowBoundCI = low.bnd,
UpBoundCI = up.bnd, row.names = NULL)
names(output)[3] = paste(100 * pe, "%_LowBoundCI")
names(output)[4] = paste(100 * pe, "%_UpBoundCI")
return(output)
}
}
|
[1] "Populations sizes are:"
A B C D E
12 76 76 32 4
Quant_Varia Pst_Values
1 QM1 0.6407570
2 QM2 0.8655181
3 QM3 0.5619511
4 QM4 0.9876724
5 QM5 0.7845236
6 QM6 0.8895326
7 QM7 0.8910552
8 Body_length 0.8903289
9 QM8 0.8337822
10 QM9 0.9553522
11 QM10 0.9844616
[1] "Populations sizes are:"
A B C E
12 76 76 4
Quant_Varia Pst_Values 95 %_LowBoundCI 95 %_UpBoundCI
1 QM2 0.8546366 0.7358223 0.93268
function (data, ci = 0, csh = 1, va = 0, boot = 1000, Pw = 0,
Rp = 0, Ri = 0, pe = 0.95)
{
nonNa.clm <- function(data, clm) {
nb.ind = dim(data)[1]
nb.na = 0
for (i in 1:nb.ind) if (is.na(data[i, clm]))
nb.na = nb.na + 1
return(nb.ind - nb.na)
}
dat.fra.prep <- function(data) {
nb.var = dim(data)[2] - 1
data = as.data.frame(data)
data[, 1] = as.character(data[, 1])
for (i in 1:nb.var) {
if (is.numeric(data[, i + 1]) == FALSE)
data[, i + 1] = as.numeric(as.character(data[,
i + 1]))
}
dat.sta <- function(dat) {
nb.vari = dim(dat)[2] - 1
st.dev = rep(0, nb.vari)
mea = rep(0, nb.vari)
for (i in 1:nb.vari) {
nna.clm = nonNa.clm(dat, i + 1)
st.dev[i] = sqrt((nna.clm - 1)/nna.clm) * sd(dat[,
i + 1], na.rm = TRUE)
mea[i] = mean(dat[, i + 1], na.rm = TRUE)
}
for (j in 1:nb.vari) dat[, j + 1] = (dat[, j + 1] -
mea[j])/st.dev[j]
return(dat)
}
data = dat.sta(data)
return(data)
}
dat.rem.ind.pop <- function(data, ind = 0, pop = 0) {
data = as.data.frame(data)
dat.rem.ind <- function(dat, ind) {
nb.rem.ind = length(ind)
nb.ind = dim(dat)[1]
for (i in 1:nb.rem.ind) dat = dat[row.names(dat)[1:(nb.ind -
i + 1)] != ind[i], ]
return(dat)
}
dat.rem.pop <- function(dat, pop) {
nb.rem.pop = length(pop)
for (i in 1:nb.rem.pop) dat = dat[dat[, 1] != pop[i],
]
return(dat)
}
if (ind[1] != 0)
data = dat.rem.ind(data, ind)
if (pop[1] != 0)
data = dat.rem.pop(data, pop)
return(data)
}
dat.pw <- function(data, pw = 0) {
if (pw[1] == 0)
return(data)
else {
data = data[data[, 1] == pw[1] | data[, 1] == pw[2],
]
return(data)
}
}
nb.pop <- function(data) {
data = data[order(data[, 1]), ]
nb.ind = dim(data)[1]
nb.pop = 1
for (i in 1:(nb.ind - 1)) if (data[i, 1] != data[i +
1, 1])
nb.pop = nb.pop + 1
return(nb.pop)
}
pop.freq <- function(data) {
data = data[order(data[, 1]), ]
nb.ind = dim(data)[1]
dat.fra = as.data.frame(data)
nb.pop = 1
for (i in 1:(nb.ind - 1)) if (data[i, 1] != data[i +
1, 1])
nb.pop = nb.pop + 1
pop.freq.vec = rep(1, nb.pop)
name = rep(0, nb.pop)
k = 1
name[1] = as.character(dat.fra[1, 1])
for (i in 2:nb.ind) if (dat.fra[i - 1, 1] == dat.fra[i,
1])
pop.freq.vec[k] = pop.freq.vec[k] + 1
else {
k = k + 1
name[k] = as.character(dat.fra[i, 1])
}
names(pop.freq.vec) = name
return(pop.freq.vec)
}
Pst.val <- function(data, csh = 1) {
nbpop = nb.pop(data)
nb.var = dim(data)[2] - 1
data = data[order(data[, 1]), ]
if (nbpop == 1)
return(rep(0, nb.var))
else {
pop.freq = pop.freq(data)
Pst.clm <- function(dat, clm) {
mea = mean(dat[, clm], na.rm = TRUE)
nna.clm = nonNa.clm(dat, clm)
SSTotal = (nna.clm - 1) * var(dat[, clm], na.rm = TRUE)
mea.pop = rep(0, nbpop)
nna.pop.freq = rep(0, nbpop)
nna.pop.freq[1] = nonNa.clm(dat[1:(pop.freq[1]),
], clm)
nb.allna.pop = 0
if (nna.pop.freq[1] == 0)
nb.allna.pop = 1
else mea.pop[1] = mean(dat[1:(pop.freq[1]), clm],
na.rm = TRUE)
for (i in 2:nbpop) {
nna.pop.freq[i] = nonNa.clm(dat[(sum(pop.freq[1:(i -
1)]) + 1):(sum(pop.freq[1:i])), ], clm)
if (nna.pop.freq[i] != 0)
mea.pop[i] = mean(dat[(sum(pop.freq[1:(i -
1)]) + 1):(sum(pop.freq[1:i])), clm], na.rm = TRUE)
else nb.allna.pop = nb.allna.pop + 1
}
SSBetween = sum(nna.pop.freq * (mea.pop - mea)^2)
SSWithin = SSTotal - SSBetween
if ((nna.clm - nbpop + nb.allna.pop) * (nbpop -
nb.allna.pop - 1) != 0) {
MSWithin = SSWithin/(nna.clm - nbpop + nb.allna.pop)
MSBetween = SSBetween/(nbpop - nb.allna.pop -
1)
return(csh * MSBetween/(csh * MSBetween + 2 *
MSWithin))
}
else {
if ((nna.clm - nbpop + nb.allna.pop) == 0)
return(1)
else return(0)
}
}
pst.val = rep(0, nb.var)
for (j in 1:nb.var) pst.val[j] = Pst.clm(data, j +
1)
return(pst.val)
}
}
boot.pst.va <- function(data, csh, boot, clm) {
nb.ind = dim(data)[1]
dat = data[, c(1, clm)]
boot.val = rep(0, boot)
for (i in 1:boot) {
da = dat[sample(1:nb.ind, nb.ind, T), ]
boot.val[i] = Pst.val(da, csh)
}
return(boot.val)
}
ConInt.pst.va <- function(data, csh, boot, clm, per) {
boot.pst.val = boot.pst.va(data = data, csh = csh, boot = boot,
clm = clm)
boot.pst.val = sort(boot.pst.val)
return(c(boot.pst.val[floor(boot * (1 - per)/2 + 1)],
boot.pst.val[ceiling(boot * (per + 1)/2)]))
}
if (va[1] == 0) {
nb.var = dim(data)[2] - 1
va = 1:nb.var
}
else {
nb.var = length(va)
for (i in 1:nb.var) {
for (j in 2:dim(data)[2]) {
if (names(data)[j] == va[i])
va[i] = j - 1
}
}
va = as.numeric(va)
if (is.na(sum(va)) == TRUE)
return("va is not valid!")
}
data = dat.fra.prep(data)
data = dat.rem.ind.pop(data, ind = Ri, pop = Rp)
data = dat.pw(data, Pw)
print("Populations sizes are:")
print(pop.freq(data))
if (ci != 1) {
output = data.frame(Quant_Varia = names(data)[va + 1],
Pst_Values = Pst.val(data, csh = csh)[va], row.names = NULL)
return(output)
}
if (ci == 1) {
low.bnd = rep(0, nb.var)
up.bnd = rep(0, nb.var)
for (i in 1:nb.var) {
ci.pst.va = ConInt.pst.va(data, csh = csh, boot = boot,
clm = va[i] + 1, per = pe)
low.bnd[i] = ci.pst.va[1]
up.bnd[i] = ci.pst.va[2]
}
output = data.frame(Quant_Varia = names(data)[va + 1],
Pst_Values = Pst.val(data, csh = csh)[va], LowBoundCI = low.bnd,
UpBoundCI = up.bnd, row.names = NULL)
names(output)[3] = paste(100 * pe, "%_LowBoundCI")
names(output)[4] = paste(100 * pe, "%_UpBoundCI")
return(output)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.