Hi all,
I​ know that there is a package QuantPsyc that has the function lm.beta to
calculate standardised coefficients. However, the function is not able to
handle categorical variables with more than 2 levels.

I coded an alternative function and hope to know if I have done anything
wrongly.
​
​Function:

lm.beta2 <- function(model) {
    model.coefficients <- summary(model)[["coefficients"]]
    # Remove intercept and extract coefficients
    model.coefficients <- model.coefficients[-grep("(Intercept)",
row.names(model.coefficients)), "Estimate"]
    df.model <- model[["model"]]
    df.model.factors <- df.model[sapply(df.model, class) == "factor"]
    df.model.factors <- model.matrix(~.-1, df.model.factors)
    df.model.factors <- df.model.factors[,colnames(df.model.factors)
%in% names(model.coefficients)]
    df.model <- cbind(df.model[,sapply(df.model, class) != "factor"],
df.model.factors)
    sx <- sapply(df.model[-1], sd)
    sy <- sapply(df.model[1], sd)
    beta <- model.coefficients * sx/sy
    return(beta)}

Example:
iris.model <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width
+Species, data = iris) lm.beta2(iris.model) Sepal.Width Petal.Length
Petal.Width 0.2610193 1.7678091 -0.2901014 Speciesversicolor
Speciesvirginica -0.4132919 -0.5846126

Did I compute the standardised coefficient correctly for Species?


​

	[[alternative HTML version deleted]]


