Skip to contents

circa_single_mixed is similar to circa_single but allows for some simple, user-specified random-effects on the rhythmic parameters of choice.

Usage

circa_single_mixed(
  x,
  col_time,
  col_outcome,
  col_id,
  randomeffects = c("k", "alpha", "phi"),
  period = 24,
  alpha_threshold = 0.05,
  nlme_control = list(),
  nlme_method = "ML",
  weights = NULL,
  suppress_all = FALSE,
  timeout_n = 10000,
  return_figure = TRUE,
  control = list()
)

Arguments

x

data.frame. This is the data.frame which contains the rhythmic data in a tidy format.

col_time

The name of the column within the data.frame, x, which contains time in hours at which the data were collected.

col_outcome

The name of the column within the data.frame, x, which contains outcome measure of interest.

col_id

The name of the column within the data.frame, x, which contains the identifying values for the random effect, such as subject_id.

randomeffects

which rhythmic parameters to allow random effects. The default is to include all rhythmic parameters.

period

The period of the rhythm. For circadian rhythms, leave this as the default value, 24.

alpha_threshold

The level of alpha for which the presence of rhythmicity is considered. Default is to 0.05.

nlme_control

A list of control values for the estimation algorithm to replace the default values returned by the function nlme::nlmeControl. Defaults to an empty list.

nlme_method

A character string. If "REML" the model is fit by maximizing the restricted log-likelihood. If "ML" the log-likelihood is maximized. Defaults to "ML".

weights

An optional numeric vector of (fixed) weights internally passed to nlme::nlme() via nlme::varPower(). When present, the objective function is weighted least squares.

suppress_all

Logical. Set to TRUE to avoid seeing errors or messages during model fitting procedure. Default is FALSE. If FALSE, also runs nlme() with verbose = TRUE.

timeout_n

The upper limit for the model fitting attempts. Default is 10000.

return_figure

Whether or not to return a ggplot graph of the rhythm and cosine model.

control

list. Used to control the parameterization of the model.

Value

list

Examples

# \donttest{
set.seed(42)
mixed_data <- function(n) {
  counter <- 1
  for (i in 1:n) {
    x <- make_data(k1 = rnorm(1, 10, 2), alpha1 = 0, phi1 = 0)
    x$id <- counter
    counter <- counter + 1
    if (i == 1) {
      res <- x
    } else {
      res <- rbind(res, x)
    }
  }
  return(res)
}
df <- mixed_data(n = 50)
out <- circa_single_mixed(
  x = df, col_time = "time", col_outcome = "measure",
  col_id = "id", randomeffects = c("k")
)
#> 
#> **Iteration 1
#> LME step: Loglik: -14555.4, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.849877 
#> varStruct  parameters:
#> power 
#>     0 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  119440.9 
#>  fixed effects: 4.871085  9.996911  6.282769  
#>  iterations: 7 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 6.330160e-01 1.068408e-08 0.000000e+00 
#> 
#> **Iteration 2
#> LME step: Loglik: -14555.4, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.849877 
#> varStruct  parameters:
#> power 
#>     0 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  119440.9 
#>  fixed effects: 4.871085  9.997704  6.283142  
#>  iterations: 2 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 7.928814e-05 2.119202e-11 0.000000e+00 
#> 
#> **Iteration 3
#> LME step: Loglik: -14555.4, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.849877 
#> varStruct  parameters:
#> power 
#>     0 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  119440.9 
#>  fixed effects: 4.871085  9.997704  6.283142  
#>  iterations: 1 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 0.000000e+00 2.400641e-16 0.000000e+00 

# with sample weights (arbitrary weights for demonstration)
sw <- runif(n = nrow(df))
out2 <- circa_single_mixed(
  x = df, col_time = "time", col_outcome = "measure",
  col_id = "id", randomeffects = c("k"), weights = sw
)
#> 
#> **Iteration 1
#> LME step: Loglik: -14555.3, nlminb iterations: 5
#> reStruct  parameters:
#>       id 
#> 1.846839 
#> varStruct  parameters:
#>        power 
#> -0.004554618 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  -9.997382  -3.14169  
#>  iterations: 5 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>       fixed    reStruct   varStruct 
#> 3.626909488 0.001645516 1.000000000 
#> 
#> **Iteration 2
#> LME step: Loglik: -14555.3, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.846838 
#> varStruct  parameters:
#>        power 
#> -0.004554618 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  -9.997382  -3.14169  
#>  iterations: 1 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 0.000000e+00 2.404592e-16 0.000000e+00 
#> 
#> **Iteration 1
#> LME step: Loglik: -14555.3, nlminb iterations: 5
#> reStruct  parameters:
#>       id 
#> 1.846839 
#> varStruct  parameters:
#>       power 
#> -0.00455462 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997689  -6.283279  
#>  iterations: 7 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>       fixed    reStruct   varStruct 
#> 0.713215604 0.001645519 1.000000000 
#> 
#> **Iteration 2
#> LME step: Loglik: -14555.3, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.846838 
#> varStruct  parameters:
#>       power 
#> -0.00455462 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997689  -6.283279  
#>  iterations: 1 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 0.000000e+00 4.809184e-16 0.000000e+00 
#> 
#> **Iteration 1
#> LME step: Loglik: -14555.3, nlminb iterations: 5
#> reStruct  parameters:
#>       id 
#> 1.846839 
#> varStruct  parameters:
#>        power 
#> -0.004554619 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997592  -0.0001218503  
#>  iterations: 5 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 6.466667e+03 1.645512e-03 1.000000e+00 
#> 
#> **Iteration 2
#> LME step: Loglik: -14555.3, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.846838 
#> varStruct  parameters:
#>        power 
#> -0.004554619 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997592  -0.0001218503  
#>  iterations: 1 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 0.000000e+00 1.202296e-16 0.000000e+00 
#> 
#> **Iteration 1
#> LME step: Loglik: -14555.3, nlminb iterations: 5
#> reStruct  parameters:
#>       id 
#> 1.846839 
#> varStruct  parameters:
#>        power 
#> -0.004554616 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997576  -0.0001508803  
#>  iterations: 4 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 1.723537e+03 1.645516e-03 1.000000e+00 
#> 
#> **Iteration 2
#> LME step: Loglik: -14555.3, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.846838 
#> varStruct  parameters:
#>        power 
#> -0.004554616 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997576  -0.0001508803  
#>  iterations: 1 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 0.000000e+00 9.618367e-16 0.000000e+00 
#> 
#> **Iteration 1
#> LME step: Loglik: -14555.3, nlminb iterations: 5
#> reStruct  parameters:
#>       id 
#> 1.846839 
#> varStruct  parameters:
#>        power 
#> -0.004554617 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  -9.997687  3.141499  
#>  iterations: 5 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>       fixed    reStruct   varStruct 
#> 3.640205877 0.001645513 1.000000000 
#> 
#> **Iteration 2
#> LME step: Loglik: -14555.3, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.846838 
#> varStruct  parameters:
#>        power 
#> -0.004554617 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  -9.997687  3.141499  
#>  iterations: 1 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 0.000000e+00 3.606888e-16 0.000000e+00 
#> 
#> **Iteration 1
#> LME step: Loglik: -14555.3, nlminb iterations: 5
#> reStruct  parameters:
#>       id 
#> 1.846839 
#> varStruct  parameters:
#>       power 
#> -0.00455462 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997639  -12.56646  
#>  iterations: 7 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>       fixed    reStruct   varStruct 
#> 0.823382885 0.001645514 1.000000000 
#> 
#> **Iteration 2
#> LME step: Loglik: -14555.3, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.846838 
#> varStruct  parameters:
#>       power 
#> -0.00455462 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997639  -12.56646  
#>  iterations: 1 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 0.000000e+00 2.286767e-13 0.000000e+00 
#> 
#> **Iteration 1
#> LME step: Loglik: -14555.3, nlminb iterations: 5
#> reStruct  parameters:
#>       id 
#> 1.846839 
#> varStruct  parameters:
#>        power 
#> -0.004554617 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997392  6.283087  
#>  iterations: 7 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>       fixed    reStruct   varStruct 
#> 1.193914453 0.001645507 1.000000000 
#> 
#> **Iteration 2
#> LME step: Loglik: -14555.3, nlminb iterations: 1
#> reStruct  parameters:
#>       id 
#> 1.846838 
#> varStruct  parameters:
#>        power 
#> -0.004554617 
#>  Beginning PNLS step: ..  completed fit_nlme() step.
#> PNLS step: RSS =  118355.2 
#>  fixed effects: 4.871032  9.997392  6.283087  
#>  iterations: 1 
#> Convergence crit. (must all become <= tolerance = 1e-05):
#>        fixed     reStruct    varStruct 
#> 0.000000e+00 3.515136e-10 0.000000e+00 
# }