Une mise-à-jour de Gitlab vers la version 14.0 est prévue le 24 juin entre 14:00 et 15:00. Le service sera inaccessible ou instable pendant cette période. Merci de votre compréhension.

.Rhistory 7.52 KB
Newer Older
Dumoulin Nicolas's avatar
Dumoulin Nicolas committed
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
install.packages("pls")
install.packages("plsdepot")
install.packages("plsRglm")
library("plsRglm", lib.loc="C:/Program Files/R/R-2.15.3/library")
setwd("D://workspace/StockedDiad/R")
library(pls)
library(plsdepot)
mydata <- read.table("D://workspace/StockedDiad/data/output/stockedDiadOutput5final.txt", sep=",", header=TRUE)
mydata$recovery = mydata$wildActiveFemaleSpawner>5
mydata1=subset(mydata, mydata$lastFailInReproduction<49.25)
# output="geoMeanEffective"
output="lastFailInReproduction"
# output="meanWildActiveFemaleSpawner"
#output = "recovery"
boxplot(mydata[, output] ~ mydata$probabilityToBirthHabitat, xlab='prob to birth habitat', ylab=output)
boxplot(mydata[, output] ~ mydata$probabilityToBirthRiver, xlab='prob to birth river')
boxplot(mydata1[, output] ~ mydata1$releaseAmount, xlab='release amount')
mydata$femaleMeanAgeAtMaturation
table(mydata[, c("recovery","femaleMeanAgeAtMaturation" )])
table(mydata[, c("recovery","Mafter")])
pls2=plsr(lastFailInReproduction ~ M0_3months + M3_12months +
M1year+ Mafter +
femaleMeanAgeAtMaturation + femaleSdAgeAtMaturation +
fecundity +
femaleMeanInterSpawningInterval +
maleMeanAgeAtMaturation + maleSdAgeAtMaturation +
maleMeanInterSpawningInterval +
probabilityToBirthRiver + probabilityToBirthHabitat +
releaseAmount + sigmaRecruitment,
ncomp = 3, data = mydata1, method = "oscorespls")
summary(pls2)
vip2 = VIP(pls2)
### VIP.R: Implementation of VIP (variable importance in projection)(*) for the
### `pls' package.
### $Id: VIP.R,v 1.2 2007/07/30 09:17:36 bhm Exp $
### Copyright © 2006,2007 Bjørn-Helge Mevik
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License version 2 as
### published by the Free Software Foundation.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
### A copy of the GPL text is available here:
### http://www.gnu.org/licenses/gpl-2.0.txt
### Contact info:
### Bjørn-Helge Mevik
### bhx6@mevik.net
### Rødtvetvien 20
### N-0955 Oslo
### Norway
### (*) As described in Chong, Il-Gyo & Jun, Chi-Hyuck, 2005, Performance of
### some variable selection methods when multicollinearity is present,
### Chemometrics and Intelligent Laboratory Systems 78, 103--112.
## VIP returns all VIP values for all variables and all number of components,
## as a ncomp x nvars matrix.
VIP <- function(object) {
if (object$method != "oscorespls")
stop("Only implemented for orthogonal scores algorithm.  Refit with 'method = \"oscorespls\"'")
if (nrow(object$Yloadings) > 1)
stop("Only implemented for single-response models")
SS <- c(object$Yloadings)^2 * colSums(object$scores^2)
Wnorm2 <- colSums(object$loading.weights^2)
SSW <- sweep(object$loading.weights^2, 2, SS / Wnorm2, "*")
sqrt(nrow(SSW) * apply(SSW, 1, cumsum) / cumsum(SS))
}
## VIPjh returns the VIP of variable j with h components
VIPjh <- function(object, j, h) {
if (object$method != "oscorespls")
stop("Only implemented for orthogonal scores algorithm.  Refit with 'method = \"oscorespls\"'")
if (nrow(object$Yloadings) > 1)
stop("Only implemented for single-response models")
b <- c(object$Yloadings)[1:h]
T <- object$scores[,1:h, drop = FALSE]
SS <- b^2 * colSums(T^2)
W <- object$loading.weights[,1:h, drop = FALSE]
Wnorm2 <- colSums(W^2)
sqrt(nrow(W) * sum(SS * W[j,]^2 / Wnorm2) / sum(SS))
}
pls2=plsr(lastFailInReproduction ~ M0_3months + M3_12months +
M1year+ Mafter +
femaleMeanAgeAtMaturation + femaleSdAgeAtMaturation +
fecundity +
femaleMeanInterSpawningInterval +
maleMeanAgeAtMaturation + maleSdAgeAtMaturation +
maleMeanInterSpawningInterval +
probabilityToBirthRiver + probabilityToBirthHabitat +
releaseAmount + sigmaRecruitment,
ncomp = 3, data = mydata1, method = "oscorespls")
summary(pls2)
vip2 = VIP(pls2)
VIP(pls2)
name(vip2[1, order(vip2[1,], decreasing = TRUE)])
names(vip2[1, order(vip2[1,], decreasing = TRUE)])
summary(pls2)
fix(pls2)
vip = VIP(pls1)
pls1=plsr(lastFailInReproduction ~ as.factor(M0_3months)+as.factor(M3_12months)+
as.factor(M1year)+ as.factor(Mafter)+
as.factor(femaleMeanAgeAtMaturation) + as.factor(femaleSdAgeAtMaturation) +
as.factor(fecundity)+
as.factor(femaleMeanInterSpawningInterval) +
as.factor(maleMeanAgeAtMaturation) + as.factor(maleSdAgeAtMaturation) +
as.factor(maleMeanInterSpawningInterval)+
as.factor(probabilityToBirthRiver)+ as.factor(probabilityToBirthHabitat) +
as.factor(releaseAmount) + as.factor(sigmaRecruitment),
ncomp = 3, data = mydata1, method = "oscorespls")
summary(pls1)
pls2=plsr(lastFailInReproduction ~ M0_3months + M3_12months +
M1year+ Mafter +
femaleMeanAgeAtMaturation + femaleSdAgeAtMaturation +
fecundity +
femaleMeanInterSpawningInterval +
maleMeanAgeAtMaturation + maleSdAgeAtMaturation +
maleMeanInterSpawningInterval +
probabilityToBirthRiver + probabilityToBirthHabitat +
releaseAmount + sigmaRecruitment,
ncomp = 3, data = mydata1, method = "oscorespls")
summary(pls2)
vip2 = VIP(pls2)
VIPij(pls2,5,5)
VIPjh(pls2,5,5)
VIPjh(pls2,1,5)
VIPjh(pls2,1,3)
VIP(pls2)
2^12
plot(RMSEP(pls2))
pls2=plsr(lastFailInReproduction ~ M0_3months + M3_12months +
M1year+ Mafter +
femaleMeanAgeAtMaturation + femaleSdAgeAtMaturation +
fecundity +
femaleMeanInterSpawningInterval +
maleMeanAgeAtMaturation + maleSdAgeAtMaturation +
maleMeanInterSpawningInterval +
probabilityToBirthRiver + probabilityToBirthHabitat +
releaseAmount + sigmaRecruitment,
ncomp = 10, data = mydata1, method = "oscorespls")
summary(pls2)
plot(RMSEP(pls2))
RMSEP(pls2)
explvar(pls2)
pls2$loading.weights
pls2$Yloadings
VIP(pls2)
barplot(vip2[1, order(vip2[1,], decreasing = TRUE)])
pls1=plsr(lastFailInReproduction ~ as.factor(M0_3months)+as.factor(M3_12months)+
as.factor(M1year)+ as.factor(Mafter)+
as.factor(femaleMeanAgeAtMaturation) + as.factor(femaleSdAgeAtMaturation) +
as.factor(fecundity)+
as.factor(femaleMeanInterSpawningInterval) +
as.factor(maleMeanAgeAtMaturation) + as.factor(maleSdAgeAtMaturation) +
as.factor(maleMeanInterSpawningInterval)+
as.factor(probabilityToBirthRiver)+ as.factor(probabilityToBirthHabitat) +
as.factor(releaseAmount) + as.factor(sigmaRecruitment),
ncomp = 10, data = mydata1, method = "oscorespls")
summary(pls1)
vip = VIP(pls1)
barplot(vip[1,order(vip[1,])])
barplot(vip[1,order(vip[1,], decreasing = TRUE)])
pls2=plsr(lastFailInReproduction ~ M0_3months + M3_12months +
M1year+ Mafter +
femaleMeanAgeAtMaturation + femaleSdAgeAtMaturation +
fecundity +
femaleMeanInterSpawningInterval +
maleMeanAgeAtMaturation + maleSdAgeAtMaturation +
maleMeanInterSpawningInterval +
probabilityToBirthRiver + probabilityToBirthHabitat +
releaseAmount + sigmaRecruitment,
ncomp = 10, data = mydata1, method = "oscorespls")
pls2=plsr(lastFailInReproduction ~ M0_3months + M3_12months +
M1year+ Mafter +
femaleMeanAgeAtMaturation + femaleSdAgeAtMaturation +
fecundity +
femaleMeanInterSpawningInterval +
maleMeanAgeAtMaturation + maleSdAgeAtMaturation +
maleMeanInterSpawningInterval +
probabilityToBirthRiver + probabilityToBirthHabitat +
releaseAmount + sigmaRecruitment,
ncomp = 10, data = mydata1, method = "oscorespls", model=TRUE)
pls2$model
pls2=plsr(lastFailInReproduction ~ M0_3months + M3_12months +
M1year+ Mafter +
femaleMeanAgeAtMaturation + femaleSdAgeAtMaturation +
fecundity +
femaleMeanInterSpawningInterval +
maleMeanAgeAtMaturation + maleSdAgeAtMaturation +
maleMeanInterSpawningInterval +
probabilityToBirthRiver + probabilityToBirthHabitat +
releaseAmount + sigmaRecruitment,
ncomp = 10, data = mydata1, method = "oscorespls", model=TRUE, validation ="CV")
summary(pls2)