cvar: Create a contextual variable for regression

View source: R/cvar.R

cvarR Documentation

Create a contextual variable for regression

Description

cvar and See also dvar are designed to be used in regression formulas to create a contextual mean of a cluster-varying variable and a 'centered-within-groups' version.

Usage

cvar(x, id, all, na.rm, FUN = mean, ...)

## S3 method for class 'factor'
cvar(x, id, all = FALSE, na.rm = TRUE, FUN = mean, ...)

## Default S3 method:
cvar(x, id, all, na.rm = TRUE, FUN = mean, ...)

Arguments

x

variable to be centered or residualized within groups. If x is a factor, cvar and dvar return matrices whose columns are named consistently with the names of coding variables for factors.

id

identifies clusters

all

(default FALSE) if TRUE cvar.factor returns the columns means of an incidence matrix including the first level. Otherwise, the first level is dropped for use in a linear model.

na.rm

(default TRUE) whether to drop missing values

FUN

(default mean) function to be applied to create contextual values

Methods (by class)

  • cvar(factor): method for class 'factor'

  • cvar(default): default method

Examples

## Not run: 
dd <- data.frame(x= 1:100, id = rep( LETTERS[1:10], each = 10))
dd$a <- factor(sample( c('a','b','c'), 100, replace = T))
dd$y <- dd$x + rep(rnorm(10), each = 10) + rnorm(100) + as.numeric(dd$a)
library(nlme)
fit <- lme( y ~ x + cvar(x,id), dd, random = ~ 1 + dvar(x,id) | id)
anova( fit , type = 'm')
# The output of 'anova' can be used to test whether a contextual variable
# should be included in the model

## End(Not run)

gmonette/spida2 documentation built on Aug. 20, 2023, 7:21 p.m.