#----------------------------------------------------------------------------
# Linear Model
# Example 1a: Continuous predictors
mod.lm1 <- lm(mpg ~ cyl + disp, data = mtcars)
std.coef(mod.lm1)
# Example 1b: Print all standardized coefficients
std.coef(mod.lm1, print = "all")
# Example 1c: Binary predictor
mod.lm2 <- lm(mpg ~ vs, data = mtcars)
std.coef(mod.lm2)
# Example 1d: Continuous and binary predictors
mod.lm3 <- lm(mpg ~ disp + vs, data = mtcars)
std.coef(mod.lm3)
# Example 1e: Continuous predictors with interaction term
mod.lm4 <- lm(mpg ~ cyl*disp, data = mtcars)
std.coef(mod.lm4)
# Example 1f: Continuous and binary predictor with interaction term
mod.lm5 <- lm(mpg ~ cyl*vs, data = mtcars)
std.coef(mod.lm5)
# Example 1g: Continuous predictor with a quadratic term
mod.lm6 <- lm(mpg ~ cyl + I(cyl^2), data = mtcars)
std.coef(mod.lm6)
if (FALSE) {
#----------------------------------------------------------------------------
# Multilevel and Linear Mixed-Effects Model
# Load lme4, nlme, and ggplot2 package
misty::libraries(lme4, nlme)
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
# Cluster mean centering, center() from the misty package
Demo.twolevel <- center(Demo.twolevel, x2, type = "CWC", cluster = "cluster")
# Grand mean centering, center() from the misty package
Demo.twolevel <- center(Demo.twolevel, w1, type = "CGM", cluster = "cluster")
# Estimate models using the lme4 package
mod1a <- lmer(y1 ~ x2.c + w1.c + (1 + x2.c | cluster), data = Demo.twolevel,
REML = FALSE)
mod2a <- lmer(y1 ~ x2.c + w1.c + x2.c:w1.c + (1 + x2.c | cluster),
data = Demo.twolevel, REML = FALSE)
# Estimate models using the nlme package
mod1b <- lme(y1 ~ x2.c + w1.c, random = ~ 1 + x2.c | cluster, data = Demo.twolevel,
method = "ML")
mod2b <- lme(y1 ~ x2.c + w1.c + x2.c:w1.c, random = ~ 1 + x2.c | cluster,
data = Demo.twolevel, method = "ML")
# Example 2: Continuous predictors
std.coef(mod1a)
std.coef(mod1b)
# Example 2: Continuous predictors with cross-level interaction
std.coef(mod2a)
std.coef(mod2b)
#----------------------------------------------------------------------------
# Example 3: Write Results into a text or Excel file
# Example 3a: Text file
std.coef(mod.lm1, write = "Std_Coef.txt", output = FALSE, check = FALSE)
# Example 3b: Excel file
std.coef(mod.lm1, write = "Std_Coef.xlsx", output = FALSE, check = FALSE)}
Run the code above in your browser using DataLab