3  La fonction as flextable

3.1 Récupérer un flextable depuis un modèle R

Les modèles usuels, tels que les modèles mixtes, les modèles GAM, les modèles linéaires généralisés mais aussi les tests et certaines classifications peuvent être transformés en flextable avec la fonction as_flextable().

3.1.1 GLM

Code
clotting <- data.frame(
    u = c(5,10,15,20,30,40,60,80,100),
    lot1 = c(118,58,42,35,27,25,21,19,18),
    lot2 = c(69,35,26,21,18,16,13,12,12))
as_flextable(glm(lot1 ~ log(u), data = clotting, family = Gamma))

Estimate

Standard Error

z value

Pr(>|z|)

(Intercept)

-0.017

0.001

-17.847

0.0000

***

log(u)

0.015

0.000

36.975

0.0000

***

Signif. codes: 0 <= '***' < 0.001 < '**' < 0.01 < '*' < 0.05

(Dispersion parameter for Gamma family taken to be 0.002446059)

Null deviance: 3.513 on 8 degrees of freedom

Residual deviance: 0.01673 on 7 degrees of freedom

3.1.2 LM

Code
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm(weight ~ group) %>% as_flextable()

Estimate

Standard Error

t value

Pr(>|t|)

(Intercept)

5.032

0.220

22.850

0.0000

***

groupTrt

-0.371

0.311

-1.191

0.2490

Signif. codes: 0 <= '***' < 0.001 < '**' < 0.01 < '*' < 0.05

Residual standard error: 0.6964 on 18 degrees of freedom

Multiple R-squared: 0.07308, Adjusted R-squared: 0.02158

F-statistic: 1.419 on 18 and 1 DF, p-value: 0.2490

3.1.3 GAM

Code
library(mgcv)
set.seed(2)

dat <- gamSim(1, n = 400, 
  dist = "normal", scale = 2, verbose = FALSE)
b <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), 
  data = dat)

ft <- as_flextable(b)
ft

Component

Term

Estimate

Std Error

t-value

p-value

A. parametric coefficients

(Intercept)

7.833

0.099

79.303

0.0000

***

Component

Term

edf

Ref. df

F-value

p-value

B. smooth terms

s(x0)

2.500

3.115

6.921

0.0001

***

s(x1)

2.401

2.984

81.858

0.0000

***

s(x2)

7.698

8.564

88.158

0.0000

***

s(x3)

1.000

1.000

4.343

0.0378

*

Signif. codes: 0 <= '***' < 0.001 < '**' < 0.01 < '*' < 0.05

Adjusted R-squared: 0.715, Deviance explained 0.725

GCV : 4.051, Scale est: 3.903, N: 400

3.1.4 Test d’hypothèse

Code
x <- rnorm(50)
y <- runif(30)
ks.test(x, y) %>% as_flextable()

statistic

p.value

method

alternative

0.6

0.0000***

Exact two-sample Kolmogorov-Smirnov test

two-sided

Signif. codes: 0 <= '***' < 0.001 < '**' < 0.01 < '*' < 0.05

3.1.5 kmeans

Code
cl <- kmeans(scale(mtcars[1:7]), 5)
ft <- as_flextable(cl)
ft

variable

1

2

3

4

5

withinss

2.1

5.0

33.4

4.1

9.6

size

3

5

14

5

5

mpg*

2.0147

0.9108

-0.8281

-0.0582

0.2571

cyl*

-1.2249

-1.2249

1.0149

-0.1050

-0.7769

disp*

-1.2551

-1.0170

0.9874

-0.5703

-0.4244

hp*

-1.2498

-0.7626

0.9120

-0.2696

-0.7714

drat*

1.5214

0.8443

-0.6869

0.4777

-0.3115

wt*

-1.3633

-1.1034

0.7992

-0.1924

-0.1239

qsec*

0.8103

0.0522

-0.6025

-0.3429

1.4915

(*) Centers

Total sum of squares: 217.0

Total within-cluster sum of squares: 54.2

Total between-cluster sum of squares: 162.8

BSS/TSS ratio: 75.0%

Number of iterations: 3

3.1.6 pam

Code
library(cluster)
dat <- as.data.frame(scale(mtcars[1:7]))
cl <- pam(dat, 3)
ft <- as_flextable(cl)
ft

variable

1

2

3

size

6

11

15

max.diss

2.2

2.8

3.2

avg.diss

1.1

1.4

1.6

diameter

2.8

4.0

5.1

separation

1.0

1.5

1.0

avg.width

0.4

0.2

0.3

mpg*

0.1509

1.1962

-0.6124

cyl*

-0.1050

-1.2249

1.0149

disp*

-0.5706

-1.2242

0.3637

hp*

-0.5351

-1.1768

0.4859

drat*

0.5675

0.9042

-0.9848

wt*

-0.3498

-1.3105

0.8715

qsec*

-0.4638

0.5883

-0.2511

(*) Centers

The average silhouette width is 0.2959

3.2 Résumé simple

L’utilisation de la fonction summarizor() suivi de as_flextable() est un moyen simple de produire des tableaux de résumé qui présentent les statistiques univariées de chaque colonne.

Une option permet d’ajouter une catégorie “global” à la fin du tableau produit.

Code
library(dplyr)
library(flextable)
z <- palmerpenguins::penguins %>% 
  select(-contains("length")) %>% 
  summarizor(
    by = "species",
    overall_label = "Overall")

ft <- as_flextable(z, spread_first_col = TRUE) %>% 
  style(i = ~!is.na(variable), pr_t = fp_text_default(bold = TRUE),
        pr_p = officer::fp_par(text.align = "left", padding = 5, line_spacing = 1.5)) %>% 
  prepend_chunks(i = ~is.na(variable), j = 1, as_chunk("\t")) %>% 
  autofit(add_w = .01)
ft

Adelie
(N=152)

Chinstrap
(N=68)

Gentoo
(N=124)

Overall
(N=344)

island

Biscoe

44 (28.9%)

0 (0.0%)

124 (100.0%)

168 (48.8%)

Dream

56 (36.8%)

68 (100.0%)

0 (0.0%)

124 (36.0%)

Torgersen

52 (34.2%)

0 (0.0%)

0 (0.0%)

52 (15.1%)

bill_depth_mm

Mean (SD)

18.3 (1.2)

18.4 (1.1)

15.0 (1.0)

17.2 (2.0)

Median (IQR)

18.4 (1.5)

18.4 (1.9)

15.0 (1.5)

17.3 (3.1)

Range

15.5 - 21.5

16.4 - 20.8

13.1 - 17.3

13.1 - 21.5

Missing

1 (0.7%)

1 (0.8%)

2 (0.6%)

body_mass_g

Mean (SD)

3,700.7 (458.6)

3,733.1 (384.3)

5,076.0 (504.1)

4,201.8 (802.0)

Median (IQR)

3,700.0 (650.0)

3,700.0 (462.5)

5,000.0 (800.0)

4,050.0 (1,200.0)

Range

2,850.0 - 4,775.0

2,700.0 - 4,800.0

3,950.0 - 6,300.0

2,700.0 - 6,300.0

Missing

1 (0.7%)

1 (0.8%)

2 (0.6%)

sex

female

73 (48.0%)

34 (50.0%)

58 (46.8%)

165 (48.0%)

male

73 (48.0%)

34 (50.0%)

61 (49.2%)

168 (48.8%)

Missing

6 (3.9%)

5 (4.0%)

11 (3.2%)

year

Mean (SD)

2,008.0 (0.8)

2,008.0 (0.9)

2,008.1 (0.8)

2,008.0 (0.8)

Median (IQR)

2,008.0 (2.0)

2,008.0 (2.0)

2,008.0 (2.0)

2,008.0 (2.0)

Range

2,007.0 - 2,009.0

2,007.0 - 2,009.0

2,007.0 - 2,009.0

2,007.0 - 2,009.0