0

First time posting here.

I'm trying to get some statistical results to output onto a Word doc using the Officer package. I understand that the body_add_* functions seem to only work on data frames. However, functions and tests like gvlma and ncvTest output as a list with unconventional dimensions so I'm unable to use the tidyr package to tidy the lists before turning them into a data frame using data.frame(). So I need help adding these block of text that are lists into a Word Document.

So far I have this as the ADF test outputs as a very nice list that is easily convertible to a data frame:

# ADF test into dataframe
adf_df = data.frame(adf)
adf_df
ft <- flextable(data = adf_df) %>%
  theme_booktabs() %>%
  autofit()

# Output table into Word doc
doc <- read_docx() %>%
  body_add_flextable(value = ft) %>%
  body_add_par(gvlma)
fileout <- "test.docx"
print(doc, target = fileout)

The body_add_par(gvlma) line gives the error:

Warning messages:
1: In if (grepl("<|>", x)) { :
  the condition has length > 1 and only the first element will be used
2: In charToRaw(enc2utf8(x)) :
  argument should be a character vector of length 1
all but the first element will be ignored

gvlma outputs as a list and here is the output:

Call:
lm(formula = PD ~ ., data = dataset)

Coefficients:
  (Intercept)  WorldBank_Oil  
        1.282         -1.449  


ASSESSMENT OF THE LINEAR MODEL ASSUMPTIONS
USING THE GLOBAL TEST ON 4 DEGREES-OF-FREEDOM:
Level of Significance =  0.05 

Call:
 gvlma(x = model) 

                    Value p-value                Decision
Global Stat        4.6172  0.3289 Assumptions acceptable.
Skewness           0.1858  0.6664 Assumptions acceptable.
Kurtosis           0.1812  0.6703 Assumptions acceptable.
Link Function      1.7823  0.1819 Assumptions acceptable.
Heteroscedasticity 2.4678  0.1162 Assumptions acceptable.
raidorz
  • 1
  • 1
  • Don't know if this is way off what you're interested in, but have you considered R markdown? https://rmarkdown.rstudio.com/ – markhogue Jul 31 '19 at 01:38
  • 1
    Hi! Thanks for your reply :) I've tried rmarkdown but I would prefer to use the Officer package first as I dont have to save a separate set of codes as .rmd – raidorz Jul 31 '19 at 02:07

1 Answers1

0

Replicating the error with iris data-set:

library(officer); library(flextable)

adf_df <- iris

ft <- flextable(data = adf_df) %>%
  theme_booktabs() %>%
  autofit()

gvlma <- lm(Petal.Length ~ Sepal.Length + Sepal.Width, data=iris)

# Output table into Word doc
doc <- read_docx() %>%
  body_add_flextable(value = ft) %>%
  body_add_par(gvlma)

Warning messages: 1: In if (grepl("<|>", x)) { : the condition has length > 1 and only the first element will be used 2: In charToRaw(enc2utf8(x)) : argument should be a character vector of length 1 all but the first element will be ignored

Issue here is that the linear model are kept as list that is efficient in calling out test parameters or model statistics. Not great as a static output.

One way to work around this is to use the commands from library(broom)

library(broom)
gvlma2 <- tidy(gvlma)
gvlma3 <- glance(gvlma)

doc <- read_docx() %>%
  body_add_flextable(value = ft) %>%
  body_add_flextable(value = flextable(gvlma2)) %>%
  body_add_flextable(value = flextable(gvlma3))


fileout <- "test.docx"
print(doc, target = fileout)

gvlma2:

# A tibble: 3 x 5
  term         estimate std.error statistic  p.value
  <chr>           <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)     -2.52    0.563      -4.48 1.48e- 5
2 Sepal.Length     1.78    0.0644     27.6  5.85e-60
3 Sepal.Width     -1.34    0.122     -10.9  9.43e-21

gvlma3:

   r.squared adj.r.squared sigma statistic  p.value    df logLik   AIC   BIC deviance df.residual
       <dbl>         <dbl> <dbl>     <dbl>    <dbl> <int>  <dbl> <dbl> <dbl>    <dbl>       <int>
 1     0.868         0.866 0.646      482. 2.74e-65     3  -146.  300.  312.     61.4         147
Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • Hi! Thanks for the help. There is a `library(gvlma)` package that I'm using to get `gvlma` function. Are you able to work from that? :) – raidorz Jul 31 '19 at 03:27
  • never come across gvlma before. `broom::tidy` doesn't seem to be able to handle it natively, though `broom::glance` still work. – Adam Quek Jul 31 '19 at 06:57