# activer l'environnement
using Pkg
Pkg.activate("env_julia_rcall") Activating project at `c:\Users\ricco\Desktop\demo\env_julia_rcall`
# activer l'environnement
using Pkg
Pkg.activate("env_julia_rcall") Activating project at `c:\Users\ricco\Desktop\demo\env_julia_rcall`
# liste des packages installés
Pkg.status()Status `C:\Users\ricco\Desktop\demo\env_julia_rcall\Project.toml` [336ed68f] CSV v0.10.16 [a93c6f00] DataFrames v1.8.2 [7073ff75] IJulia v1.34.4 [6f49c342] RCall v0.14.13 [f3b207a7] StatsPlots v0.15.8
# lire le fichier CSV
using CSV
import DataFrames as DFR
df = CSV.read("media_prof_afc.csv", DFR.DataFrame; delim=";")
# dimensions
DFR.size(df)(8, 7)
# affichage
df| Row | Prof | Radio | Televis | Quot_Nat | Quot_Reg | Press_Mag | Press_TV |
|---|---|---|---|---|---|---|---|
| String15 | Int64 | Int64 | Int64 | Int64 | Int64 | Int64 | |
| 1 | Agriculteur | 96 | 118 | 2 | 71 | 50 | 17 |
| 2 | Petit_Patr | 122 | 136 | 11 | 76 | 49 | 41 |
| 3 | Prof_Cad_Sup | 193 | 184 | 74 | 63 | 103 | 79 |
| 4 | Prof_Inter | 360 | 365 | 63 | 145 | 141 | 184 |
| 5 | Employe | 511 | 593 | 57 | 217 | 172 | 306 |
| 6 | Ouvr_Qualif | 385 | 457 | 42 | 174 | 104 | 220 |
| 7 | Ouvr_Non_Qualif | 156 | 185 | 8 | 69 | 42 | 85 |
| 8 | Inactif | 1474 | 1931 | 181 | 852 | 642 | 782 |
# accès à la librairie
using RCall
# vérification de la version de R
R"R.version.string"RObject{StrSxp}
[1] "R version 4.5.2 (2025-10-31 ucrt)"
# envoyer le dataframe vers R via @rput
@rput df| Row | Prof | Radio | Televis | Quot_Nat | Quot_Reg | Press_Mag | Press_TV |
|---|---|---|---|---|---|---|---|
| String15 | Int64 | Int64 | Int64 | Int64 | Int64 | Int64 | |
| 1 | Agriculteur | 96 | 118 | 2 | 71 | 50 | 17 |
| 2 | Petit_Patr | 122 | 136 | 11 | 76 | 49 | 41 |
| 3 | Prof_Cad_Sup | 193 | 184 | 74 | 63 | 103 | 79 |
| 4 | Prof_Inter | 360 | 365 | 63 | 145 | 141 | 184 |
| 5 | Employe | 511 | 593 | 57 | 217 | 172 | 306 |
| 6 | Ouvr_Qualif | 385 | 457 | 42 | 174 | 104 | 220 |
| 7 | Ouvr_Non_Qualif | 156 | 185 | 8 | 69 | 42 | 85 |
| 8 | Inactif | 1474 | 1931 | 181 | 852 | 642 | 782 |
# vérification "dans" R
# comment définir une séquence de commandes R
R"""
str(df)
"""'data.frame': 8 obs. of 7 variables:
$ Prof : chr "Agriculteur" "Petit_Patr" "Prof_Cad_Sup" "Prof_Inter" ...
$ Radio : int 96 122 193 360 511 385 156 1474
$ Televis : int 118 136 184 365 593 457 185 1931
$ Quot_Nat : int 2 11 74 63 57 42 8 181
$ Quot_Reg : int 71 76 63 145 217 174 69 852
$ Press_Mag: int 50 49 103 141 172 104 42 642
$ Press_TV : int 17 41 79 184 306 220 85 782
RObject{NilSxp}
NULL
# réorganisation du data frame pour l'AFC
# attention, on ne peut pas mettre des commentaires # dans le code R
R"""
row.names(df) <- df$Prof
df <- df[-1]
df
"""RObject{VecSxp}
Radio Televis Quot_Nat Quot_Reg Press_Mag Press_TV
Agriculteur 96 118 2 71 50 17
Petit_Patr 122 136 11 76 49 41
Prof_Cad_Sup 193 184 74 63 103 79
Prof_Inter 360 365 63 145 141 184
Employe 511 593 57 217 172 306
Ouvr_Qualif 385 457 42 174 104 220
Ouvr_Non_Qualif 156 185 8 69 42 85
Inactif 1474 1931 181 852 642 782
# librairie ade4
# à installer sous R au préalable si besoin
R"""
library(ade4)
packageVersion("ade4")
"""RObject{VecSxp}
[1] '1.7.23'
# lancer l'AFC de ADE4
# afficher les résultats
R"""
res <- dudi.coa(df,scannf=FALSE)
summary(res)
"""Class: coa dudi
Call: dudi.coa(df = df, scannf = FALSE)
Total inertia: 0.02228
Eigenvalues:
Ax1 Ax2 Ax3 Ax4 Ax5
1.386e-02 7.211e-03 8.247e-04 3.039e-04 8.269e-05
Projected inertia (%):
Ax1 Ax2 Ax3 Ax4 Ax5
62.1982 32.3650 3.7018 1.3638 0.3712
Cumulative projected inertia (%):
Ax1 Ax1:2 Ax1:3 Ax1:4 Ax1:5
62.20 94.56 98.27 99.63 100.00
RObject{NilSxp}
NULL
# récuperer les coordonnées lignes
# on a un data frame
R"""
c_lig <- res$li
"""
# et récupérer les résultats sous Julia avec @rget
@rget c_lig
# vérfication sous Julia
println(c_lig)8×2 DataFrame Row │ Axis1 Axis2 │ Float64 Float64 ─────┼──────────────────────── 1 │ -0.16615 -0.309612 2 │ -0.0683771 -0.143151 3 │ 0.429976 -0.0608726 4 │ 0.106603 0.0325968 5 │ -0.0157286 0.0954689 6 │ -0.0437125 0.101379 7 │ -0.117797 0.0948642 8 │ -0.0325837 -0.0333952
# faire de même pour les colonnes
R"""
c_col <- res$co
"""
# et envoyer à julia
@rget c_col
# vérfication sous Julia
println(c_col)6×2 DataFrame Row │ Comp1 Comp2 │ Float64 Float64 ─────┼────────────────────────── 1 │ 0.0149402 0.022106 2 │ -0.0532829 0.002081 3 │ 0.540675 -0.00620677 4 │ -0.108828 -0.109645 5 │ 0.0948126 -0.132497 6 │ -0.00975363 0.161609
# graphique sous Julia
import StatsPlots as STP
# position des modalités lignes dans le premier plan factoriel
STP.scatter(c_lig.Axis1, c_lig.Axis2,
aspect_ratio=:equal, xlims=(-0.6,+0.6), ylims=(-0.6,+0.6), size=(600,600),
label="", xlabel="F1", ylabel="F2", framestyle=:box, markersize=0,
title="Graphique des modalités lignes x colonnes",
series_annotations=(df.Prof,STP.font(7,:green)) # ici pour mettre les étiquettes
)
# position des modalités colonnes
STP.scatter!(c_col.Comp1, c_col.Comp2,
label="", markersize=0,
series_annotations=(names(df)[2:end],STP.font(7,:blue))
)
# lignes centrales
STP.hline!([0], linestyle = :dash, color = :gray,label="")
STP.vline!([0], linestyle = :dash, color = :gray,label="")