Articles

Install R 3.4.1 and Rstudio on ubuntu 16.04 LTS

It is now available using apt-get. Install R 3.4.1 on ubuntu 16.04 LTS cd $HOME wget https://cran.rstudio.com/src/base/R-3/R-3.4.1.tar.gz tar xvf R-3.4.1.tar.gz cd R-3.4.1 sudo apt-get install gcc  fort77  aptitude sudo aptitude install g++ sudo aptitude install xorg-dev sudo aptitude install libreadline-dev sudo aptitude install gfortran gfortran --version sudo apt install libpcre3-dev  liblzma-dev  libbz2-dev sudo add-apt-repository ppa:webupd8team/java -y sudo apt-get update sudo apt-get install oracle-java8-installer sudo apt-get install oracle-java8-set-default sudo apt-get install texlive-base sudo apt-get install texinfo sudo apt-get install  texlive-fonts-extra sudo apt-get install libcurl4-openssl-dev sudo apt-get install libcairo2-dev libpng-dev zlib1g-dev libpixman-1-dev libxt-dev java -version # need to build shared library (--enable-R-shlib) # the --enable-R-shlib option is needed for the RStudio IDE to work sudo ./configure --enable-R-shlib ...

Install ou update RcppArmadillo package in MacOSX Sierra

If the update is done with clang++ installed with Xcode, installation will fail. You must install a specific version of clang++. First install homebrew, using in terminal: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" Then install llvm brew install --with-clang llvm Then, you will find clang++ on /usr/local/opt/llvm/bin/clang++. You can add these lines in the .profile: PATH="/usr/local/opt/llvm/bin:${PATH}" export PATH Run R in terminal, and in R, enter: install.packages("RcppArmadillo") The correct clang++ will be find and compilation works. Note that different versions of clang++ are used if you run RStudio or R in terminal: In RStudio: > system("clang++ --version") Apple LLVM version 8.1.0 (clang-802.0.42) Target: x86_64-apple-darwin16.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin In R...

Draw a trend arrow with confidence interval

Image
Let draw an arrow with its width being dependent on the confidence interval: drawTrend <- function(x, y, show.circle=TRUE, show.0=TRUE) {   model <- lm(y ~ x)   direction.se <- coef(summary(model))["x", "Std. Error"]   direction <- model$coefficients["x"]    library("car") k <- ellipse(center=c(0.5, 0), radius=c(0.5, 0.5),               shape=matrix(data = c(1, 0, 0, 1), nrow=2),               segments=1001, draw = FALSE) par(mar=c(0, 0, 0, 0)) plot(x = c(0, 0), y=c(0, 0), type="n", bty="n", xlim=c(-0.1,1.1),       ylim=c(-0.6, 0.6), las=1, asp=1,      xaxt="n", yaxt="n", xlab="", ylab = "") if (show.circle) polygon(k[, 1], k[, 2], lwd=1) maxy <- which.max(k[, 2]) maxx <- which.max(k[, 1]) miny <- which.min(k[, 2]) minx <- which.min(k[, 1]) if (show.circle) { s <- seq(from=maxy, to=maxx, leng...

Two solutions for progress bar... not equivalent

To make a progress bar, you have two main solutions: In library utils: txtProgressBar() In library progress: progress_bar$new() The second one makes a progress bar with automatic "estimated time to arrival". This is very useful. However, it does that functionality at a cost. Let do 3 examples: without progress bar, with a txtProgressBar() and with a progress_bar$new() (see below for the codes). The code is ran in Rstudio, R GUI and in terminal under MacOSX: Rstudio: > A[3]/R[3]  elapsed  8.235853  > B[3]/R[3]  elapsed  1.835827  R GUI: > A[3]/R[3]  elapsed  7.895495  > B[3]/R[3]  elapsed  1.181143  R in terminal: > A[3]/R[3]  elapsed  10.74719  > B[3]/R[3]  elapsed  2.079948  In conclusion, the progress_bar$new() is between 5 to 8 time slower than the txtProgressBar(). It is nicer but it has a cost ! R <- system.time({  ...

RStudio or not RStudio... that is the question!

RStudio is a very useful environment for development in R. However, running your script in RStudio can take much much more time than running the same in R. Let do an example with a progress bar. Surprisingly R GUI is the fastest solution, second is RStudio and slowest is R in terminal. library("progress") In RStudio: > system.time({ +     Total <- 50000 +     pb <- progress_bar$new( +         format = "  completion [:bar] :percent eta: :eta", +         total = Total, clear = FALSE) +     for (annee in 1:Total) { +         pb$tick() +         Sys.sleep(0.0001) +     } + })   completion [========================================================================] 100% eta:  0s utilisateur     système      écoulé       49.652       1.222     117.707  In R GU...

Standard error from Hessian Matrix... what can be done when problem occurs

An Hessian matrix is a square matrix of partial second order derivative. From the square-root of the inverse of the diagonal, it is possible to estimate the standard error of parameters. Let take an example: val=rnorm(100, mean=20, sd=5) # Return -ln L of values in val in Gaussian distribution with mean and sd in par fitnorm<-function(par, val) {   -sum(dnorm(val, par["mean"], par["sd"], log = TRUE)) } # Initial values for search p<-c(mean=20, sd=5) # fit the model result <- optim(par=p, fn=fitnorm, val=val, method="BFGS", hessian=TRUE) # Inverse the hessian matrix to get SE for each parameters mathessian <- result$hessian inversemathessian <- solve(mathessian) res <- sqrt(diag(inversemathessian)) # results data.frame(Mean=result$par, SE=res)   library(MASS) (r<-fitdistr(val, "normal")) It works well. However, the inverse of the matrix cannot be calculated if the second order derivative for some paramete...

Integer random number: comparison of two strategies

If you need an integer ransom number, here are two strategies. First get it from uniform distribution and take the integer part, second generate all integer possible values and take one randomly. The first solution is two times faster. As seen in the last example, the manipulation of a vector of 50000 elements is not efficient at all. However, if you know how many random numbers you will need, it is better to estimate first a long vector of random numbers and after get one per one. 1/ Using integer of uniform random number one by one: 1.994 > system.time( +   for (i in 1:1000000) +     a <- floor(runif(n = 1, min=1, max=50001)) + ) utilisateur     système      écoulé        1.853       0.116       1.994 2/ Long vector of integer of uniform random number: 0.196 > system.time({+   ru <- floor(runif(n = 1000000, min=1, max=50001)) +   for (i in 1:1000000...