Articles

Affichage des articles du mai, 2022

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