Non linear mixed effect models

Interesting read: 


http://sia.webpopix.org/nlme.html

#original data was 500 observations, 2 varying variables HOMAR and INSULIN. 3 time periods 


reshaped_data<-reshape(test.reshape, 

        direction = "long",

        varying = c("HOMA_M0", "HOMA_M3","HOMA_M6", "INSULIN_M0", "INSULIN_M3", "INSULIN_M6"),

        v.names = c("HOMAR", "INSULIN"),

        idvar = c("id"),

        timevar = "visit",

        new.row.names = 1:1500,

        times = 1:3)


Colors in R

 http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/

Function to calculate CI of linear regression coefficients

Amateur code

```

 jad<-function(x, y) {

  model<-lm(y~x)

  std.err<-coef(summary(model))[, 2]

  coef.model1<-coef(summary(model))[, 1]

  upper.ci<-coef.model1+1.96*std.err

  lower.ci<-coef.model1-1.96*std.err

  print(upper.ci)

  print(lower.ci)

}

```

Pro code

jad <- function(x, y) {
  `colnames<-`(
    coef(summary(lm(y ~ x)))[,1:2] %*% matrix(c(1, 1.96, 1, -1.96), nrow = 2), c("upper.ci", "lower.ci")
  )
}

Some data management Libraries for R

Library (magrittr)

Library (dplyr)



https://uc-r.github.io/pipe#:~:text=The%20compound%20assignment%20%25%25,and%20then%20assigning%20the%20result.&text=The%20exposition%20(%20%25%24%25%20)%20operator%20is,to%20some%20of%20the%20columns.




wrs%>%

extract(1:20,c("age", "edu")) %>%

head


or

wrs%>%

extract(1:20,1:2) %>%

head


Practical way to label factors


```
library(memisc)
labels(anemia$anem)<-c("not anemic"=0, "anemic"=1) #library memisc
```

How to generate a simulated data in R


Package {simstudy}

The below example uses the library(simstudy) to generate a data of 10 observation for 3 variables: nr, x1 and y1. The function defData allows us to define the name, the formula and the distribution of each variable.
Check CRAN webiste for additional documentation on the simstudy package.

```
def <- defData(varname = "nr", dist = "nonrandom", formula = 7, id = "idnum")
def <- defData(def, varname = "x1", dist = "uniform", formula = "10;20")
def <- defData(def, varname = "y1", formula = "nr + x1 * 2", variance = 8)

dt <- genData(10, def)

dt

```

Introduction to the Analysis of Survival Data in the Presence of Competing Risks

 https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4741409/