adjust.linearly.std: ADJUST LINEARLY and STANDARIZE

Description Usage Arguments Value Examples

Description

Adjust a dependent variable y linearly to several independent variables x1, x2 ... and standardize afterwards the adjusted values. Requirements to the data: Homoscedasticity and Linearity of y ~ x...

Usage

1

Arguments

formula

A formula that gives the relationship of the adjustment.

data

A dataframe which contains the dependent and the independent variables.

Value

A dataframe containing a new column with the adjusted and standardized values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
hlpr4life::load.pkgs( c( "hlpr4life", "ggplot2" ) )
set.seed( 1 )
num <- 100
sexes <- c( "female", "male" )
d <- data.frame( sex = sample( sexes, num, T, prob = c( .3, .7 )  ), age = round( runif( num, 0, 20 ), 1 ) )
d$y <- c( 30, 35 )[ match( d$sex, sexes ) ] + c( 2, 3 )[ match( d$sex, sexes ) ] * d$age + rnorm( num, 0, 10 )
d <- adjust.linearly.std( y ~ age, d )
d <- adjust.linearly.std( y ~ sex, d )
d <- adjust.linearly.std( y ~ age * sex, d )
thm          <- list( theme_bw( ), geom_point( ), geom_smooth( method = "lm" ) )
thm.brown    <- list( theme_bw( ), geom_point( col = "green3" ), geom_smooth( col = "green3", method = "lm" ) )
thm.bicol    <- list.append( thm, scale_color_manual( values = c( "firebrick1", "steelblue3" ), guide = F ) )
thm.bicolfac <- list.append( thm.bicol, facet_grid( sex ~ . ) )
ggsubplot(
  ggplot( d, aes( age, y ) ) + thm.brown,
  ggplot( d, aes( age, y, col = sex ) ) + thm.bicol,
  ggplot( d, aes( age, y.std.for.age ) ) + thm.brown,
  ggplot( d, aes( age, y.std.for.age, col = sex ) ) + thm.bicol,
  ggplot( d, aes( age, y.std.for.sex ) ) + thm.brown,
  ggplot( d, aes( age, y.std.for.sex, col = sex ) ) + thm.bicol,
  ggplot( d, aes( age, y.std.for.age.sex, col = sex ) ) + thm.bicol,
  ggplot( d, aes( age, y.std.for.age.sex, col = sex ) ) + thm.bicolfac,
  cols = 4 )

TPeschel/hlpr4life documentation built on May 9, 2019, 2:25 p.m.