mwelz / GenericML

R implementation of Generic Machine Learning Inference (Chernozhukov, Demirer, Duflo and Fernández-Val, 2020).
GNU General Public License v3.0
60 stars 14 forks source link

Make sure printed output always ends with a new line #25

Closed aalfons closed 2 years ago

aalfons commented 2 years ago

When using cat() to print the last line of some output, make sure that the line ends with a line break ("\n"). The RStudio console adds this line break if it is missing, but that is not true for other R consoles, and we should not rely on that.

For example, this is the output of get_best() when I run R in the MacOS terminal:

> library("GenericML")
Loading required package: ggplot2
Loading required package: mlr3
Loading required package: mlr3learners
> ## generate data
> set.seed(1)
> n  <- 150                                  # number of observations
> p  <- 5                                    # number of covariates
> D  <- rbinom(n, 1, 0.5)                    # random treatment assignment
> Z  <- matrix(runif(n*p), n, p)             # design matrix
> Y0 <- as.numeric(Z %*% rexp(p) + rnorm(n)) # potential outcome without treatment
> Y1 <- 2 + Y0                               # potential outcome under treatment
> Y  <- ifelse(D == 1, Y1, Y0)               # observed outcome
> 
> ## column names of Z
> colnames(Z) <- paste0("V", 1:p)
> 
> ## specify learners
> learners <- c("tree", "mlr3::lrn('ranger', num.trees = 10)")
> 
> ## perform generic ML inference
> # small number of splits to keep computation time low
> x <- GenericML(Z, D, Y, learners, num_splits = 2,
+                parallel = FALSE)
> 
> ## access best learner
> get_best(x)
                                     lambda lambda.bar
tree                                0.01096      5.512
mlr3::lrn('ranger', num.trees = 10) 0.11812      5.303
---
The best learner for BLP is mlr3::lrn('ranger', num.trees = 10) with lambda = 0.1181. 
The best learner for GATES and CLAN is mlr3::lrn('ranger', num.trees = 10) with lambda.bar = 5.5124.> 

Note how the command prompt > is on the last line of the output instead of a new line, since the last cat() statement in the print method doesn't contain a line break.

On a side note, you don't need to use cat(paste0(...)), just use cat(..., sep = "").

mwelz commented 2 years ago

Good catch, thank you! I'll fix it in a moment.