Modérateur : Groupe des modérateurs
Code : Tout sélectionner
str(chcp)
'data.frame': 400 obs. of 16 variables:
$ CONTEXTE: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ F1 : Factor w/ 2 levels "0","1": 1 1 2 1 1 1 1 1 1 1 ...
$ MD3 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ S1 : Factor w/ 2 levels "0","1": 1 1 2 1 1 1 1 1 1 1 ...
$ MD1 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 2 ...
$ MD5 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ T1 : Factor w/ 2 levels "0","1": 2 1 1 1 1 1 1 1 1 1 ...
$ VL1C5 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 2 ...
$ VL4C5 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ...
$ VL2C5 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ...
$ VL3C5 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ...
$ PG.1C7 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ E : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 2 1 1 1 ...
$ MA4 : Factor w/ 2 levels "0","1": 2 1 1 2 1 1 2 1 2 1 ...
$ MA5 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ N : Factor w/ 2 levels "0","1": 1 1 1 2 1 1 2 1 2 1 ...
Code : Tout sélectionner
chcp <- readXL("/Users/melaniehenriques/Desktop/stats Faux resume.xlsx",
rownames=TRUE, header=TRUE, na="", sheet="cod2 > =20 ind (2)",
stringsAsFactors=TRUE)
str(chcp)
for (i in 1:16) chcp[,i]=as.factor(chcp[,i])
str(chcp)
chcp_fct <- function(tab=chcp,n1=211,n0=149,fac01=CONTEXTE){
tab0 <- subset(chcp,CONTEXTE==COUP) * [4] ERREUR: objet 'COUP' introuvable
tab1 <- subset(chcp,CONTEXTE==CHUTE) * [5] ERREUR: objet 'CHUTE' introuvable
newtab0 <- tab0[sample(1:nrow(tab0),n0),]
newtab1 <- tab1[sample(1:nrow(tab1),n1),]
perm <- rbind(newtab0,newtab1)
return(perm)
}
app <- chcp[perm,]
valid <- chcp[-perm,]
library(randomForest)
set.seed(1234)
foret <- randomForest(CONTEXTE~., data = app)
foret
plot(foret)
Code : Tout sélectionner
chcp <- readXL("/Users/melaniehenriques/Desktop/stats Faux resume.xlsx",
rownames=TRUE, header=TRUE, na="", sheet="cod2 > =20 ind (3)",
stringsAsFactors=TRUE)
str(chcp)
for (i in 1:16) chcp[,i]=as.factor(chcp[,i])
str(chcp)
chcp_fct <- function(tab=chcp,n1=211,n0=149,fac01=CONTEXTE){
tab0 <- subset(chcp,CONTEXTE==0)
tab1 <- subset(chcp,CONTEXTE==1)
newtab0 <- tab0[sample(1:nrow(tab0),n0),]
newtab1 <- tab1[sample(1:nrow(tab1),n1),]
perm <- rbind(newtab0,newtab1)
return(perm)
}
app <- chcp[perm,] * [6] ERREUR: objet 'perm' introuvable
valid <- chcp[-perm,]
library(randomForest)
set.seed(1234)
foret <- randomForest(CONTEXTE~., data = app)
foret
plot(foret)
Code : Tout sélectionner
reprex::reprex({
chcp <- readXL(
"/Users/melaniehenriques/Desktop/stats Faux resume.xlsx",
rownames = TRUE,
header = TRUE,
na = "",
sheet = "cod2 > =20 ind (2)",
stringsAsFactors = TRUE
)
str(chcp)
for (i in 1:16) {
chcp[, i] <- as.factor(chcp[, i])
}
str(chcp)
chcp_fct <- function(tab = chcp, n1 = 211, n0 = 149, fac01 = CONTEXTE) {
tab0 <- subset(chcp, CONTEXTE == COUP)
tab1 <- subset(chcp, CONTEXTE == CHUTE)
newtab0 <- tab0[sample(1:nrow(tab0), n0), ]
newtab1 <- tab1[sample(1:nrow(tab1), n1), ]
perm <- rbind(newtab0, newtab1)
return(perm)
}
app <- chcp[perm, ]
valid <- chcp[-perm, ]
library(randomForest)
set.seed(1234)
foret <- randomForest(CONTEXTE ~ ., data = app)
foret
plot(foret)
})
Code : Tout sélectionner
chcp <- readXL(
"/Users/melaniehenriques/Desktop/stats Faux resume.xlsx",
rownames = TRUE,
header = TRUE,
na = "",
sheet = "cod2 > =20 ind (2)",
stringsAsFactors = TRUE
)
#> Error in readXL("/Users/melaniehenriques/Desktop/stats Faux resume.xlsx", : could not find function "readXL"
str(chcp)
#> Error in str(chcp): object 'chcp' not found
for (i in 1:16) {
chcp[, i] <- as.factor(chcp[, i])
}
#> Error in is.factor(x): object 'chcp' not found
str(chcp)
#> Error in str(chcp): object 'chcp' not found
chcp_fct <- function(tab = chcp, n1 = 211, n0 = 149, fac01 = CONTEXTE) {
tab0 <- subset(chcp, CONTEXTE == COUP)
tab1 <- subset(chcp, CONTEXTE == CHUTE)
newtab0 <- tab0[sample(1:nrow(tab0), n0), ]
newtab1 <- tab1[sample(1:nrow(tab1), n1), ]
perm <- rbind(newtab0, newtab1)
return(perm)
}
app <- chcp[perm, ]
#> Error in eval(expr, envir, enclos): object 'chcp' not found
valid <- chcp[-perm, ]
#> Error in eval(expr, envir, enclos): object 'chcp' not found
library(randomForest)
#> randomForest 4.6-14
#> Type rfNews() to see new features/changes/bug fixes.
set.seed(1234)
foret <- randomForest(CONTEXTE ~ ., data = app)
#> Error in eval(m$data, parent.frame()): object 'app' not found
foret
#> Error in eval(expr, envir, enclos): object 'foret' not found
plot(foret)
#> Error in plot(foret): object 'foret' not found
Code : Tout sélectionner
> library(readxl)
> chcp <- read_excel("~/Desktop/aide stat.xlsx")
> str(chcp)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 6 obs. of 7 variables:
$ IND : num 17 18 19 36 37 38
$ CONTEXTE: chr "COUP" "COUP" "COUP" "CHUTE" ...
$ F1 : num 0 0 0 0 0 1
$ MD3 : num 0 0 0 0 0 0
$ S1 : num 0 0 0 0 0 0
$ MD1 : num 0 0 1 0 0 0
$ MD5 : num 0 0 0 0 0 0
> for (i in 1:6) chcp[,i]=as.factor(chcp[,i])
Erreur : Must use a vector in `[`, not an object of class matrix.
Run `rlang::last_error()` to see where the error occurred.
> str(chcp)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 6 obs. of 7 variables:
$ IND : num 17 18 19 36 37 38
$ CONTEXTE: chr "COUP" "COUP" "COUP" "CHUTE" ...
$ F1 : num 0 0 0 0 0 1
$ MD3 : num 0 0 0 0 0 0
$ S1 : num 0 0 0 0 0 0
$ MD1 : num 0 0 1 0 0 0
$ MD5 : num 0 0 0 0 0 0
> data.frame(chcp)
IND CONTEXTE F1 MD3 S1 MD1 MD5
1 17 COUP 0 0 0 0 0
2 18 COUP 0 0 0 0 0
3 19 COUP 0 0 0 1 0
4 36 CHUTE 0 0 0 0 0
5 37 CHUTE 0 0 0 0 0
6 38 CHUTE 1 0 0 0 0
> dput(chcp)
structure(list(IND = c(17, 18, 19, 36, 37, 38), CONTEXTE = c("COUP",
"COUP", "COUP", "CHUTE", "CHUTE", "CHUTE"), F1 = c(0, 0, 0, 0,
0, 1), MD3 = c(0, 0, 0, 0, 0, 0), S1 = c(0, 0, 0, 0, 0, 0), MD1 = c(0,
0, 1, 0, 0, 0), MD5 = c(0, 0, 0, 0, 0, 0)), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
Code : Tout sélectionner
library(readxl)
chcp <- read_excel([color=#FF0000]"~/Desktop/aide stat.xlsx"[/color])
str(chcp)
for (i in 1:6) chcp[,i]=as.factor(chcp[,i])
str(chcp)
data.frame(chcp)
dput(chcp)
chcp_fct <- function(tab=chcp,n1=2,n0=1,fac01=CONTEXTE){
tab0 <- subset(chcp,CONTEXTE==COUP)
tab1 <- subset(chcp,CONTEXTE==CHUTE)
newtab0 <- tab0[sample(1:nrow(tab0),n0),]
newtab1 <- tab1[sample(1:nrow(tab1),n1),]
perm <- rbind(newtab0,newtab1)
return(perm)
}
app <- chcp[perm,]
valid <- chcp[-perm,]
library(randomForest)
set.seed(1234)
foret <- randomForest(CONTEXTE~., data = app)
foret
plot(foret)
Code : Tout sélectionner
for (i in 1:6) chcp[,i]=as.factor(chcp[,i])
#> Erreur : Must use a vector in `[`, not an object of class matrix.
Code : Tout sélectionner
app <- chcp[perm,]
#> Error in `[.tbl_df`(chcp, perm, ): objet 'perm' introuvable
Code : Tout sélectionner
chcp_fct <- function(tab = chcp, n1 = 2, n0 = 1, fac01 = CONTEXTE) {
tab0 <- subset(chcp, CONTEXTE == COUP)
tab1 <- subset(chcp, CONTEXTE == CHUTE)
newtab0 <- tab0[sample(1:nrow(tab0), n0), ]
newtab1 <- tab1[sample(1:nrow(tab1), n1), ]
rbind(newtab0, newtab1)
}
Code : Tout sélectionner
chcp <- structure(
list(
IND = c(17, 18, 19, 36, 37, 38),
CONTEXTE = c("COUP", "COUP", "COUP", "CHUTE", "CHUTE", "CHUTE"),
F1 = c(0, 0, 0, 0, 0, 1),
MD3 = c(0, 0, 0, 0, 0, 0),
S1 = c(0, 0, 0, 0, 0, 0),
MD1 = c(0, 0, 1, 0, 0, 0),
MD5 = c(0, 0, 0, 0, 0, 0)
),
row.names = c(NA, -6L),
class = c("tbl_df", "tbl", "data.frame")
)
Code : Tout sélectionner
chcp_fct <- function(tab, n1, n0, fac01) {
tab0 <- tab[tab[, fac01] == "COUP", ]
tab1 <- tab[tab[, fac01] == "CHUTE", ]
rbind(
tab0[sample(1:nrow(tab0), n0), ],
tab1[sample(1:nrow(tab1), n1), ]
)
}
Code : Tout sélectionner
chcp_fct(chcp, 2, 1, "CONTEXTE")
#> IND CONTEXTE F1 MD3 S1 MD1 MD5
#> 2 18 COUP 0 0 0 0 0
#> 6 38 CHUTE 1 0 0 0 0
#> 5 37 CHUTE 0 0 0 0 0
Code : Tout sélectionner
chcp <- data.frame(
IND = 1L:200L,
CONTEXTE = rep(c("COUP", "CHUTE"), each = 100L),
stringsAsFactors = FALSE
)
Code : Tout sélectionner
set.seed(200420)
chcp_90 <- do.call("rbind",
by(
data = chcp,
INDICES = chcp[["CONTEXTE"]],
FUN = function(data) data[sample(1:nrow(data), floor(0.9 * nrow(data))), ]
)
)
nrow(chcp_90)
#> [1] 180
Code : Tout sélectionner
library("dplyr")
set.seed(200420)
chcp_90 <- chcp %>%
group_by(CONTEXTE) %>%
sample_frac(0.9) %>%
ungroup()
nrow(chcp_90)
#> [1] 180
Retourner vers « Questions en cours »
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 1 invité