Articles

Affichage des articles du 2022

install sf et terra in MacOSX with ARM processor

In terminal: brew install proj brew install sqlite3 in R: install.packages("sf", type = "source", configure.args = c("--with-sqlite3-lib=/opt/homebrew/opt/sqlite/lib", "--with-proj-lib=/opt/homebrew/opt/proj/lib")) install.packages("terra", type = "source", configure.args = c("--with-sqlite3-lib=/opt/homebrew/opt/sqlite/lib", "--with-proj-lib=/opt/homebrew/opt/proj/lib")) or Check the last version here: https://CRAN.R-project.org/package=terra and do install.packages("http://cran.r-project.org/src/contrib/terra_1.7-71.tar.gz", repos=NULL, type="source", configure.args = c("--with-sqlite3-lib=/opt/homebrew/opt/sqlite/lib", "--with-proj-lib=/opt/homebrew/opt/proj/lib"))

After Homebrew installs

 To use the bundled libc++ please add the following LDFLAGS:   LDFLAGS="-L/opt/homebrew/opt/llvm/lib/c++ -Wl,-rpath,/opt/homebrew/opt/llvm/lib/c++" llvm is keg-only, which means it was not symlinked into /opt/homebrew, because macOS already provides this software and installing another version in parallel can cause all kinds of trouble. If you need to have llvm first in your PATH, run:   echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc For compilers to find llvm you may need to set:   export LDFLAGS="-L/opt/homebrew/opt/llvm/lib"   export CPPFLAGS="-I/opt/homebrew/opt/llvm/include" poppler-qt5 is keg-only, which means it was not symlinked into /opt/homebrew, because it conflicts with poppler. If you need to have poppler-qt5 first in your PATH, run:   echo 'export PATH="/opt/homebrew/opt/poppler-qt5/bin:$PATH"' >> ~/.zshrc For compilers to find poppler-qt5 you may need to set:   export LDFLAGS

Error in install packages about libnetcdf.so.15 not found (Ubuntu 22.04.01): a solution

If you install netcdf library using: sudo apt-get install libapparmor1 libcurl4-openssl-dev netcdf-bin libnetcdf-dev udunits-bin libudunits2-dev It will install the current version of libnetcdf in  /usr/lib/x86_64-linux-gnu/ It can be seen with : cd  /usr/lib/x86_64-linux-gnu/ ls -al libnetcdf* -rw-r--r-- 1 root root    1200 sept. 29  2021 libnetcdf.settings lrwxrwxrwx 1 root root      15 sept. 29  2021 libnetcdf.so -> libnetcdf.so.19 -rw-r--r-- 1 root root 1346880 sept. 29  2021 libnetcdf.so.19 But when you try to install or update  tidync  package, it will produce an error: Erreur dans dyn.load(file, DLLpath = DLLpath, ...) :    impossible de charger l'objet partagé '/usr/local/lib/R/site-library/ncdf4/libs/ncdf4.so':   libnetcdf.so.15: cannot open shared object file: No such file or directory It search for libnetcdf.so.15 which is an outdated version. The solution is to symlink libnetcdf.so.15 with the most recent version which is itself a symlink ! sudo ln -s /us

Managing seed for random number

set.seed(423) runif(3) # [1] 0.1089715 0.5973455 0.9726307 # Save the seed oldseed <- .Random.seed runif(3) # [1] 0.7973768 0.2278427 0.5189830 # Restore the seed .Random.seed <- oldseed runif(3) # [1] 0.7973768 0.2278427 0.5189830

install most recent version of libgeos in ubuntu

First remove all previous installations of libgeos using: sudo apt remove libgeos* sudo updatedb You can also search for other installation using locate libgeos or locate geos Remove any installation or spurious ln file. Normally it will permit to remove this kind of message in R: WARNING: different compile-time and run-time versions of GEOS Compiled with:3.12.0-CAPI-1.18.0 Running with:3.10.2-CAPI-1.16.0 (check the most recent version of the lib in the web site  https://libgeos.org/usage/download/ )  If the 3.12.1 version is the most recent: wget https://download.osgeo.org/geos/geos-3.12.1.tar.bz2 tar xvfj geos-3.12.1.tar.bz2 cd geos-3.12.1 mkdir _build cd _build # Set up the build cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local  .. # Run the build, test, install make ctest sudo make install After you can remove the files: cd ../.. rm -rf geos-3.12.1 rm -rf geos-3.12.1.tar.bz2  Then add this in .profile GEOS_LIBRARY_PATH='/usr/lib/x86_64-linux-gnu/lib/libgeos_c

install rerddap on ubuntu 22.04

sudo apt-get install netcdf-bin libnetcdf-dev sudo updatedb locate libnetcdf.so (copy the link) sudo ln -s /usr/lib/x86_64-linux-gnu/libnetcdf.so /usr/lib/libnetcdf.so sudo ln -s /usr/lib/x86_64-linux-gnu/libnetcdf.so /usr/lib/libnetcdf.so.15 Then in R install.packages("rerddap")

Periodic GLM or GLMM

 Let take an example: # generate data  jo <- 1:800  trans_trigo <- 10+sin((jo/365.25)*2*pi)*1+ cos((jo/365.25)*2*pi)*5  obs <- rnorm(n=800, mean=trans_trigo, sd=2)    # plot the data  plot(jo, obs, bty="n", xlim=c(1,800), ylim=c(0,20))  par(new=TRUE)  plot(jo, trans_trigo, bty="n", xlim=c(1,800), ylim=c(0,20), xlab="", ylab="",        axes=FALSE, col="violet", type="l", lwd=2)    # group is per month observation  mydat <- data.frame(obs=obs, jour=jo, jour2=jo*jo, group=round(jo/27))    # glm with ordinal day as linear factor  mod <- glm(obs ~ jour, data=mydat, family=gaussian)  summary(mod)  newdata <- data.frame(jour=1:800)  preddf2 <- predict(mod, type="response", newdata=newdata, se.fit=FALSE)  par(new=TRUE)  plot(jo, preddf2, bty="n", xlim=c(1,800),  ylim=c(0,20), xlab="", ylab="", axes=FALSE, col="red", type="l")    # glm with ordinal day as

To have names of months in English and not in current langage version of R

 > Sys.setlocale(category = "LC_TIME", locale="en_GB.UTF-8") [1] "en_GB.UTF-8"  > x <- seq(from=as.Date("2000-04-01"), length.out=100, by=1)  > y <- rnorm(length(x), 5,2)  > plot(x, y)  > Sys.setlocale(category = "LC_TIME", locale="") [1] "fr_FR.UTF-8"

To install spam package in MacOSX

 To install spam package in MacOSX, I have been forced to do: sudo ln -s /usr/local/opt/gcc/lib/gcc/current /usr/local/gfortran/lib  to prevent an error: ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin18/8.2.0' ld: warning: directory not found for option '-L/usr/local/gfortran/lib' ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin18/8.2.0' ld: warning: directory not found for option '-L/usr/local/gfortran/lib' ld: library not found for -lgfortran clang: error: linker command failed with exit code 1 (use -v to see invocation)

Using only a common lib folder for all users in Ubuntu

In R, you cat get the environment variables: > Sys.getenv("R_HOME") [1] "/usr/lib/R" > Sys.getenv("R_ENVIRON") [1] "" > Sys.getenv("R_PROFILE") [1] "" > Sys.getenv("R_PROFILE_USER") [1] "" The library folder are defined here: > Sys.getenv("R_LIBS_USER") [1] "/home/agirard/R/x86_64-pc-linux-gnu-library/4.2" > Sys.getenv("R_LIBS_SITE") [1] "/usr/local/lib/R/site-library/:/usr/lib/R/site-library:/usr/lib/R/library" This is what you get with: .libPaths() In  > system(paste0("ls -al ", file.path(Sys.getenv("R_HOME"), "etc"))) total 16 drwxr-xr-x 2 root    root    4096 juil. 27 20:20 . drwxrwxrwx 8 mgirond mgirond 4096 juil. 28 13:25 .. -rw-r--r-- 1 root    root     234 juin  26 22:22 javaconf lrwxrwxrwx 1 root    root      14 juin  26 22:22 ldpaths -> /etc/R/ldpaths lrwxrwxrwx 1 root    root      15 juin  26 22:22 Makec

Exponential model fitting

Let an exponential model Nt=N0*exp(r*t) be fitted. What are the different solutions taking into account different hidden hypotheses: There are several ways to generate the data and depending the way the data are generated, different model should be used. t <- 11:110 N0 <- 100 r <- 0.05 Error proportional of N(t); if the constant of proportionality (here 0.2) is not too large the probability to have negative value is low y <- N0*exp(t*r) set.seed(1) Nt <- rnorm(100, mean=y, sd=0.2*y) df <- data.frame(t=t, Nt=Nt) Note that the error is not independent on the mean: heteroskedasticity ggplot(data = df,         aes(x = .data[["t"]],             y = .data[["Nt"]])) + geom_line() +theme_bw() Note that in log scale, the error is constant ggplot(data = df,         aes(x = .data[["t"]],             y = log(.data[["Nt"]]))) + geom_line() +theme_bw() Or an alternative is to model the data using a constant error and then data are homoskedast

The SD of a random variable itself being a random variable

Imagine a hierarchical model with 2 random variables of sd0 and sd1 standard deviation. What is the global SD ? It is simply: SD= sqrt(sd0^2+sd1^2) sd0 <- 1 sd1 <- 2 x0 <- rnorm(100000, mean=100, sd=sd0) sd(x0) var(x0) x1 <- rnorm(100000, mean=x0, sd=sd1) sd(x1) var(x1) sd0^2+sd1^2 sqrt(sd0^2+sd1^2)

Difference between c() and append()

 append() can be used instead of c(), but it is not recommended as c() is much more rapid than append(): > microbenchmark({m <- c(0, c(0, 3))}, times = 1E6) Unit: nanoseconds                        expr min  lq     mean median  uq     max neval  {     m <- c(0, c(0, 3)) } 288 349 450.4081    355 393 6536440 1e+06 > microbenchmark({m <- append(0, append(0, 3))}, times = 1E6) Unit: microseconds                                  expr   min    lq     mean median    uq      max neval  {     m <- append(0, append(0, 3)) } 1.488 1.763 2.178313  1.868 2.033 12825.54 1e+06 However, append() has one option more:  x <- c(10,8,20) c(x, 6) # always adds to the end # [1] 10 8 20 6 append(x, 6, after = 2) # [1] 10  8  6 20

Managing class and their order

# result is of class character; it prints correctly result <- "Je suis donc je pense" class(result) print(result) # I add a new class but I keep character class. All is ok # The order of classes is not important because there is no print() function for class ECFOCF class(result) <- unique(append(class(result),"ECFOCF"))) class(result) print(result) result <- "Je suis donc je pense" class(result) <- unique(append("ECFOCF", class(result))) class(result) print(result) # I create a new function for class ECFOCF # This function is used in priority whatever the ordre of classes print.ECFOCF <- function(x) {cat("print.ECFOCF ", x)} result <- "Je suis donc je pense" print(result) class(result) <- unique(append(class(result),"ECFOCF"))) class(result) print(result) result <- "Je suis donc je pense" class(result) <- unique(append("ECFOCF", class(result))) class(result) print(result

GoogleEarth and R

 Introduction to Google Earth Engine with R language https://www.youtube.com/watch?v=SHXuIpjU3YE Deep Learning for Remote Sensing images with R language https://www.youtube.com/watch?v=N3CHgRlRqOA

Credible interval for median of Weibull distribution

Image
Alternative proposed by Varin Sacha using the package weibulltools  with delta method (a little bit modified to present only the median and the CI): library(weibulltools) x <- rweibull(100000, 2, 2) data <- reliability_data(x = x, status = 1)  ml <- ml_estimation(x = data, distribution = "weibull") conf <- confint_fisher(x = ml, b_lives = 0.5, direction = "x")  The results are: > as.numeric(conf[conf$prob == 0.5, c(4, 1, 5)]) [1] 1.657227 1.663275 1.669346 The true median was : > 2*log(2)^(1/2) [1] 1.665109 Let try if the average estimate by Fisher method for 1,000 samples is biased or not: library(weibulltools) library(pbmcapply) k <- pbmclapply(1:1000, FUN=function(i) { x <- rweibull(100000, 2, 2) data <- reliability_data(x = x, status = 1)  ml <- ml_estimation(x = data, distribution = "weibull") conf <- confint_fisher(x = ml, b_lives = 0.5, direction = "x")  return(as.numeric(conf[conf$prob == 0.5, 1])) }, mc.c

Information to compile packages

From a discussion in   I am attempting to build my package using devtools::build I have verified that both Rcpp and RcppArmadillo are working properly. However, when I build, I get the following error message  Error: package or namespace load failed for ‘HACSim’ in dyn.load(file, DLLpath = DLLpath, ...):     unable to load shared object '/private/var/folders/wv/4_z4h7ns57g7qvd600qgd__w0000gn/T/RtmpGe2Cqt/Rinst118e1e9f13a0/00LOCK-HACSim/00new/HACSim/libs/HACSim.so':  dlopen(/private/var/folders/wv/4_z4h7ns57g7qvd600qgd__w0000gn/T/RtmpGe2Cqt/Rinst118e1e9f13a0/00LOCK-HACSim/00new/HACSim/libs/HACSim.so, 6): Symbol not found: ___addtf3      Referenced from: /usr/local/lib/libquadmath.0.dylib      Expected in: /usr/local/lib/libgcc_s_x86_64.1.dylib     in /usr/local/lib/libquadmath.0.dylib    Error: loading failed    Execution halted    ERROR: loading failed ─  removing ‘/private/var/folders/wv/4_z4h7ns57g7qvd600qgd__w0000gn/T/RtmpGe2Cqt/Rinst118e1e9f13a0/HACSim’          -----------

Configure Rstudio server

 sudo vi /etc/rstudio/rserver.conf # Server Configuration File #  www-port=8080 rsession-which-r=usr/bin/R auth-timeout-minutes=0 auth-stay-signed-in-days=3000 Note that the option session-timeout-minutes=0 Does not work. Perhaps it is only for the pro version

Install broom and spdep from source in MacOSX 12.2

I'm having trouble with : install.packages("broom", type = "source") and install.packages("spdep", type = "source") in R 4.12 in MacOSX 12.2 The error message is: Erreur dans dyn.load(file, DLLpath = DLLpath, ...) :    impossible de charger l'objet partagé '/Library/Frameworks/R.framework/Versions/4.1/Resources/library/glue/libs/glue.so':   dlopen(/Library/Frameworks/R.framework/Versions/4.1/Resources/library/glue/libs/glue.so, 0x0006): Library not loaded: /usr/local/opt/gsl/lib/libgsl.25.dylib   Referenced from: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/glue/libs/glue.so   Reason: tried: '/usr/local/opt/gsl/lib/libgsl.25.dylib' (no such file), '/Library/Frameworks/R.framework/Resources/lib/libgsl.25.dylib' (no such file), '/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/server/libgsl.25.dylib' (no such file), '/usr/local/Cellar/gsl/2.7.1/lib/libgsl.25.dylib

Install mixcat package in MacOSX and Ubuntu

 In ubuntu 20.04: sudo apt-get install libgsl0-dev install.packages("mixcat") In MacOSX: brew install gsl   Create a directory ~/.R/ if it does not exist and a file Makervars with PKG_LIBS=-L/usr/local/opt/gettext/lib CFLAGS=-I/usr/local/opt/gsl/include LDFLAGS=-L/usr/local/opt/gsl/lib -lgsl -lgslcblas Then in R: install.packages("mixcat") After the installation, remove the Makervars file or it will create interference with other installation, for example nloptr. Thanks to aymeric.stamm at math.cnrs.fr to have found this solution to install the nloptr package !

Install sf and spdep from source in MacOSX

I had problem to install new version of spdep from source. I get this answer from maintainer of the package: This is not an spdep issue. See r-spatial/sf#1848, and the resolution of never installing from source unless you know how to do it and need to do it to access the development versions of packages, or more recent versions of external software. I don't agree at all with this answer. When you build a package from source, it is exactly fitted for your configuration. Here is the solution: In terminal brew install gdal brew install proj brew install geos brew install sqlite3 or brew reinstall *** in R with intel processor install.packages("sf",configure.args = "--with-gdal-config=/usr/local/bin/gdal-config", type="source") install.packages("spdep",configure.args = "--with-gdal-config=/usr/local/bin/gdal-config", type="source") And it works In R with ARM64 processor install.packages("sf", type = "source"

Fit observations with skew normal distribution or two truncated normal distributions

Image
 The skew normal distribution is based on the work of: Azzalini, A., 1985. A class of distributions which includes the normal ones. Scand. J. Statist. 12, 171-178. The two truncated normal distributions is based on the work of: Sicard, O., 2013. La loi normale asymétrique. Définition, premières propriétés, estimation des paramètres. Pour que l’asymétrie ne soit plus considérée comme anormale... Laboratoire d'informatique et de mathématiques, La Réunion. The skew normal distribution is available in sn package in CRAN. The asymetric Gaussian distribution is not available. It is easy to create this function; it has 3 parameters, mean, sdlow and sdhigh. If sdlow==sdhigh, the asymetric Gaussian distribution is equal to Gaussian distribution. dnorm2 <- function(x, mean = 0, sdlow = 1, sdhigh = 1, sd=NULL, log = FALSE) {   if (!is.null(sd)) {     sdlow <- sd     sdhigh <- sd   }   return(dnorm(x = x, mean=mean, sd=ifelse(x < mean, sdlow, sdhigh), log = log)) } fitnorm2 <- f