Articles

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 o...

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...

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) #...