Thanks for these elements. I will investigate some of them.
The purpose is for me to transform "chr" vector to factor vector for bnlearn after reading the data from excel. (and not from csv where the dataframe is directly "factor")
In other word,the question, in reading data from Excel file, would be :
==> Why does stringsAsFactors not default to TRUE ????
Thanks also for the "stringsasfactors-an-unauthorized-biography/"... which gives the idea to investigate on the function "...as.factor " or something approaching...
As a reprex, here is the simplified set of R commands:
Script:
library(bnlearn)
library(lattice)
library(gRain)
library(readxl)
setwd("My_Dir") # working dir
Donnees <- read_excel("My_data.xlsx", sheet = "RB_FLAP") #
str(Donnees)
Gives:
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 54 obs. of 7 variables:
# $ thick : chr "petit" "moyen" "gros" "petit" ...
# $ Bshaft: chr "fin" "fin" "fin" "medium" ...
# $ length: chr "court" "court" "court" "court" ...
# $ Ribs : chr "non" "non" "non" "non" ...
# $ Strain: chr "].2-.4]" "[0-.2]" "[0-.2]" "].2-.4]" ...
# $ Utotal: chr "].5-1]" "[0.-.5]" "[0.-.5]" "].5-1]" ...
# $ MRFY : chr "]1.3-2]" "]1.3-2]" "]1.3-2]" "]1.3-2]" ...
Script:
ratio_LV<-0.8 # Learning 80%
nbligne=nrow(Donnees)
listal<-sort(sample(nbligne,round(nbligne*ratio_LV)))
Donnees.Learn <- Donnees[listal, ] # data.frame Learning
Donnees.Valid<-Donnees[-listal, ] # data.frame validation
#### Problem comes with "bnlearn & "chr" type in "Donnees"
res_hc <- hc(Donnees.Learn,whitelist=NULL,debug=TRUE, score = "aic")
results:
Error in data.type(x) :
variable thick is not supported in bnlearn (type: character).
So, I am now looking for function converting data type "chr" to "factor"
Thanks again for the time spent on this