model_mean: Get Mean results from Adult model Change Model

View source: R/model_mean.R

model_meanR Documentation

Get Mean results from Adult model Change Model

Description

Gets survey means svymean, standard error and confidence interval estimates of adult_weight or child_weight.

Usage

model_mean(model, meanvars = names(model)[-which(names(model) %in% c("Time",
  "BMI_Category", "Correct_Values", "Model_Type"))], days = seq(0,
  length(model[["Time"]]) - 1, length.out = 25), group = rep(1,
  nrow(model[[meanvars[1]]])), design = NA, confidence = 0.95)

Arguments

model

(list) List from adult_weight or adult_weight.

Optional

meanvars

(vector) Strings indicating which variables are required to estimate the mean.

days

(vector) Vector of days in which to compute the estimates

group

(vector) Variable in which to group the results.

design

A survey.design object. See svydesign for additional information on design objects.

confidence

(numeric) Confidence level (default = 0.95)

Details

The default design is that of simple random sampling.

Author(s)

Dalia Camacho-GarcĂ­a-FormentĂ­ daliaf172@gmail.com

Rodrigo Zepeda-Tello rzepeda17@gmail.com

Examples

#EXAMPLE 1A: RANDOM SAMPLE MODELLING FOR ADULTS
#--------------------------------------------------------

#Antropometric data
models <- c(45, 67, 58, 92, 81)
heights <- c(1.30, 1.73, 1.77, 1.92, 1.73)
ages    <- c(45, 23, 66, 44, 23)
sexes   <- c("male", "female", "female", "male", "male") 

#Matrix of energy consumption reduction: 
EIchange <- rbind(rep(-100, 365), rep(-200, 365), rep(-200, 365), 
                  rep(-123, 365), rep(-50, 365))

#Create model change model
model_model <- adult_weight(models, heights, ages, sexes, 
                             EIchange)
                             
#Calculate survey mean and variance for 25 days
aggregate_data <- model_mean(model_model)

#You can plot the mean with ci
## Not run: 
if(require(ggplot2)){
ggplot(subset(aggregate_data, variable == "Body_model")) + 
    geom_line(aes(x = time, y = mean)) +
    geom_line(aes(x = time, y = Lower_CI_mean), linetype = "dashed") +
    geom_line(aes(x = time, y = Upper_CI_mean), linetype = "dashed") +
    theme_classic() + xlab("Days") + ylab("Mean Body model (kg)")
}

#EXAMPLE 1C: RANDOM SAMPLE MODELLING FOR CHILDREN
#--------------------------------------------------------
#Antropometric data
FatFree <- c(32, 17.2, 18.8, 20, 24.1)
Fat     <- c(4.30, 2.02, 3.07, 1.12, 2.93)
ages    <- c(10, 6.2, 5.4, 4, 4.1)
sexes   <- c("male", "female", "female", "male", "male") 

#Returns a model change matrix and other matrices
model_model <- child_weight(ages, sexes, Fat, FatFree)

#Calculate survey mean and variance for 25 days
aggregate_data <- model_mean(model_model)

#You can plot the mean with ci
if(require(ggplot2)){
ggplot(subset(aggregate_data, variable == "Body_model")) + 
    geom_line(aes(x = time, y = mean)) +
    geom_line(aes(x = time, y = Lower_CI_mean), linetype = "dashed") +
    geom_line(aes(x = time, y = Upper_CI_mean), linetype = "dashed") +
    theme_classic() + xlab("Days") + ylab("Mean Body model (kg)")
}
  
#EXAMPLE 2A: SURVEY DATA FOR ADULTS
#-------------------------------------------------------
#Data frame for use in survey
probs   <- runif(10, 20, 60)
datasvy <- data.frame(
  id    = 1:10,
  bw    = runif(10,60,90),
  ht    = runif(10, 1.5, 2),
  age   = runif(10, 18, 80),
  sex   = sample(c("male","female"),10, replace = TRUE),
  kcal  = runif(10, 2000, 3000),
  group = sample(c(0,1), 10, replace = TRUE),
  svyw  = probs/sum(probs))

#Days
days <- 365

#Energy intake matrix
EIchange <- matrix(NA, nrow = 0, ncol = days)
for(i in 1:nrow(datasvy)){
    EIchange <- rbind(EIchange, rep(datasvy$kcal[i], days))
}

#Calculate model change                   
svymodel <- adult_weight(datasvy$bw, datasvy$ht, datasvy$age, 
                          datasvy$sex, EIchange)
                          
#Create survey design using survey package                           
design <- survey::svydesign(id = ~id, models = datasvy$svyw, 
data = datasvy)

#Group to calculate means
group  <- datasvy$group     
    
#Calculate survey mean and variance for 25 days
aggregate_data <- model_mean(svymodel, design = design, group = group)

#You can plot the mean with ci
if(require(ggplot2)){
ggplot(subset(aggregate_data, variable == "Body_model")) + 
    geom_ribbon(aes(x = time, ymin = Lower_CI_mean, ymax = Upper_CI_mean,
    fill = factor(group)), alpha = 0.25) +
    geom_line(aes(x = time, y = mean, color = factor(group)), size = 2) +
    theme_classic() + xlab("Days") + ylab("Mean Body model (kg)") 
}
#EXAMPLE 2A: SURVEY DATA FOR CHILDREN
#-------------------------------------------------------
#Data frame for use in survey
probs   <- runif(10, 20, 60)
datasvy <- data.frame(
  id      = 1:10,
  age     = runif(10, 2, 12),
  sex     = sample(c("male","female"),10, replace = TRUE),
  fat     = runif(10, 2, 10),
  fatfree = runif(10, 8, 15),
  group   = sample(c(0,1), 10, replace = TRUE),
  svyw    = probs/sum(probs))

#Days
days <- 365

#Calculate model change                   
svymodel <- child_weight(datasvy$age, datasvy$sex, datasvy$fat, datasvy$fatfree)
                          
#Create survey design using survey package                           
design <- survey::svydesign(id = ~id, models = datasvy$svyw, 
data = datasvy)

#Group to calculate means
group  <- datasvy$group     
    
#Calculate survey mean and variance for 25 days
aggregate_data <- model_mean(svymodel, design = design, group = group)
aggregate_data
#You can plot the mean with ci
if(require(ggplot2)){
ggplot(subset(aggregate_data, variable == "Body_model")) + 
    geom_ribbon(aes(x = time, ymin = Lower_CI_mean, ymax = Upper_CI_mean,
    fill = factor(group)), alpha = 0.25) +
    geom_line(aes(x = time, y = mean, color = factor(group)), size = 2) +
    theme_classic() + xlab("Days") + ylab("Mean Body model (kg)") 
}                     

## End(Not run)                                                             

INSP-RH/bw documentation built on July 3, 2023, 6:50 p.m.