contr.slide: Sliding contrast / backward difference coding

contr.slideR Documentation

Sliding contrast / backward difference coding

Description

Similiar to the base contrast functions (e.g., contr.sum), this coding scheme is known as 'sliding contrast coding' or 'backward difference coding'. This factor coding scheme compares the mean of the dependent variable on one level to the mean of the previous level. This function with return a matrix of contrasts that follow this scheme. Evidently this is similar (if not identical) to the function contr.sdif.

Usage

contr.slide(n, contrasts = TRUE, sparse = FALSE)

Arguments

n

A vector of levels for a factor, or the number of levels.

contrasts

A logical indicating whether contrasts should be computed.

sparse

A logical indicating if the result should be sparse (of class dgCMatrix-class), using package Matrix.

Examples

n<-1000
ys <- c(rnorm(n, mean = 0, sd = 50),
        rnorm(n, mean = 100, sd = 50),
        rnorm(n, mean = 100, sd = 50),
        rnorm(n, mean = 5, sd = 50))
dists <- c(rep("A",n),
           rep("B",n),
           rep("C",n),
           rep("D",n))
df <- data.frame(
    y<-ys,
    fac<-factor(dists)
)
# Default coding
summary(lm(y~fac,data=df))

contrasts(df$fac) <- contr.slide(4)/4
# With sliding contrast coding
summary(lm(y~fac,data=df))


burchill/zplyr documentation built on Feb. 2, 2023, 11:01 a.m.