Articles

Install plumber server

This is intended for a Ubuntu 20.04 server: sudo apt-get install git-core libssl-dev libcurl4-gnutls-dev curl libsodium-dev libxml2-dev sudo R install.packages("plumber") q("no") mkdir plumberapp cd plumberapp vi plumber.R ## Copy these lines # plumber.R #* Echo back the input #* @param msg The message to echo #* @get /echo function(msg="") {   list(msg = paste0("The message is: '", msg, "'")) } ## End vi myapi.R ## Copy these lines r <- plumber::plumb("/home/mgirond/plumberapp/plumber.R") r$run(host="0.0.0.0", port=8000) ## End vi my-api.service ## Copy these lines [Unit] Description=plumber service [Service] ExecStart=/usr/bin/Rscript /home/mgirond/plumberapp/myapi.R [Install] WantedBy=multi-user.target ## End sudo ufw allow 8000/tcp cp my-api.service /etc/systemd/system/my-api.service systemctl start my-api systemctl enable my-api sudo reboot Then it is possible to test the API: curl "http://localh...

open the working directory as a window in finder

Simply enter: system(command=sprintf('open %s', shQuote(getwd()))) It has been introduced in HepersMG package > 4.5-1 as the function openwd() https://CRAN.R-project.org/package=HelpersMG   install.packages("https://hebergement.universite-paris-saclay.fr/marcgirondot/CRAN/HelpersMG.tar.gz", repos=NULL, type="source") library(HelpersMG) openwd() It should work also for windows with  shell.exec(getwd())

Reset "par" with defaut values

Enter in R or Rstudio and do: dput(par(no.readonly = TRUE)) Copy the list() that is shown, for example for me: list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE, ask = FALSE,     bg = "white", bty = "o", cex = 1, cex.axis = 1, cex.lab = 1,     cex.main = 1.2, cex.sub = 1, col = "black", col.axis = "black",     col.lab = "black", col.main = "black", col.sub = "black",     crt = 0, err = 0L, family = "", fg = "black", fig = c(0,     1, 0, 1), fin = c(9.47674418604651, 8.72093023255814), font = 1L,     font.axis = 1L, font.lab = 1L, font.main = 2L, font.sub = 1L,     lab = c(5L, 5L, 7L), las = 0L, lend = "round", lheight = 1,     ljoin = "round", lmitre = 10, lty = "solid", lwd = 1, mai = c(1.02,     0.82, 0.82, 0.42), mar = c(5.1, 4.1, 4.1, 2.1), mex = 1,     mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L, 1L), mfrow = c(1L,     1L), mgp = c(3, 1, 0), mkh = 0.001, new = FAL...

Install tiff, ijtiff and jpeg packages in Ubuntu and MacOSX

In terminal, first install wget to download sources of the packages. Check the most recent sources in CRAN web site. brew install wget For tiff packages, in MacOSX, Intel computer  brew install libtiff sudo ln -s  /usr/local/include/tiff.h /Library/Frameworks/R.framework/Resources/include/ sudo ln -s /usr/local/include/tiffconf.h /Library/Frameworks/R.framework/Resources/include/ sudo ln -s /usr/local/include/tiffio.h /Library/Frameworks/R.framework/Resources/include/ sudo ln -s /usr/local/include/tiffvers.h /Library/Frameworks/R.framework/Resources/include/ sudo ln -s /usr/local/lib/libtiff* /Library/Frameworks/R.framework/Resources/lib/ cd $HOME wget https://cran.r-project.org/src/contrib/tiff_0.1-8.tar.gz R CMD INSTALL --configure-vars='INCLUDE_DIR=/usr/local/include LIB_DIR=/usr/local/lib' tiff_0.1-8.tar.gz rm tiff_0.1-8.tar.gz For ijtiff In R: install.packages(c("checkmate", "strex", "zeallot")) In terminal:  wget https://cran....

Setup new installation of R in MacosX

Adapted from  https://ryanhomer.github.io/posts/build-openmp-macos-catalina-complete Install R from  cran.r-project.org At the end of ~/.zshrc add:  export PATH=$PATH:/Library/Frameworks/R.framework/Versions/Current/Resources/bin/ Then you can use R within the terminal. Install macbrew via: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" Then in ~/.Rprofile: marcgirondot@MacBook-Pro-de-Marc ~ % cat .Rprofile # Default CRAN mirror options(repos=structure(c(CRAN="https://cloud.r-project.org/"))) # Path to run R with system Sys.setenv(PATH=paste(Sys.getenv("PATH"), "/Library/Frameworks/R.framework/Versions/Current/Resources/bin/", sep=":")) Install llvm brew install llvm libomp Install GCC and gettext brew install gcc gettext To configure R to build packages with the versions of clang and gcc you just installed, set up a Makevars file. This must be located at ~/.r/Makevars. Some parts do no w...

Generate a pseudo-Hessian matrix from MCMC series

The Hessian matrix at a point P (P is a vector) is the matrix of partial derivatives at this point. It is often used after non-linear optimization and the standard error of parameters can be obtained using the square-root of the diagonal of the inverse of the Hessian matrix. But when you are estimate SE like this, you lost the covariances between parameters. Then if you estimate random numbers using the SE of parameters, the original structure is lost. It is much better to estimate random numbers directly from the Hessian matrix. Then you keep the variance-covariance structure. When the model is fitted using MCMC, no Hessian matrix is directly available. A pseudo-Hessian matrix can be calculated using: hessian <- solve(cov( [matrix of values for different iterations] )) Let do an example with the fit of a normal distribution: First using maximum likelihood: val <- rnorm(30, 10, 2) library(MASS) ft <- fitdistr(val, densfun="normal") The Hessian matrix is not available...

Confidence interval to check for difference: Use 1.96 SE and not 1.96 SD !

Let create two variables of length 100, one with mean 10 (A) and one with mean 12 (B) both with SD=2. Of course the two variables overlap. A <- rnorm(100, 10, 2) B <- rnorm(100, 12, 2) library(HelpersMG) barplot_errbar(c(mean(A) ,mean(B)), errbar.y = c(1.96*sd(A), 1.96*sd(B)), las=1, ylim=c(0, 20), main="1.96 x SD") Now do a t test. It is highly significant: t.test(A, B) Welch Two Sample t-test data:  A and B t = -7.8344, df = 197.93, p-value = 2.82e-13 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval:  -2.737632 -1.636588 sample estimates: mean of x mean of y   9.741912 11.929021  Then if you want use the overall of confidence interval, use SE: barplot_errbar(c(mean(A) ,mean(B)), errbar.y = c(1.96*sd(A)/sqrt(length(A)),   1.96*sd(B)/sqrt(length(A))), las=1, ylim=c(0, 12), main="1.96 x SE")