# Why different results for linear regression in sem() from lavaan and lm()?

thanks for reading this.

I am a student trying to understand more about statistics for social science questions. I am trying to build equivalence in R between the sem() function in {lavvan} and the lm() function in {stats} for a simple linear regression with one outcome and one predictor, both of which are not latent variables. However, even though I could obtain the same parameter estimate for the model, the estimated standard errors are slightly different, no matter which estimator I use. An example is attached below:

#load libraries
library(lavaan)

#Create a data frame
set.seed(1231)
n <- 100 #number of observations

X <- rnorm(n, mean = 50, sd = 10)
Y <- rnorm(n, mean = 65, sd = 8)

df <- data.frame(X, Y)

#using the lm() function
lm <- lm(Y ~ X, data = df)
summary(lm)

#using the sem() function from lavaan
model_sem <- 'Y~X'
sem <- sem(model_sem, estimator = "ML", se = "standard", data = df)
summary(sem, nd = 7)


Results from the current example:

1. From the lm() output
            Estimate Std. Error t value Pr(>|t|)
(Intercept) 67.30819    4.37739   15.38   <2e-16
X           -0.04116    0.08571   -0.48    0.632

1. From the sem() output
Regressions:
Estimate      Std.Err      z-value      P(>|z|)
Y ~
X                -0.0411622    0.0848524   -0.4851035    0.6276029


The parameter estimate is equal while the SEs for the parameter and the p values are slight different. I also inspected the variance-covariance matrix from both models using vcov() function in {stats4}.

vcov(lm)[c("X"),c("X")]
[1] 0.00734687
vcov(sem)[c("Y~X"),c("Y~X")]
[1] 0.007199932


I tried to hand calculate the variance-covariance matrix from the lm() output and successfully did it. However, I cannot find functions to extract the observed (or expected) information matrix from sem() or calculate the log-likelihood function that was used according to the lavaan documentation. Is it possible to achieve equivalence between the two in this specific case? Why (where did I do wrong) or why not (what is the difference, is it the model estimation and specification or just due to the packages)?

Thank you so much!

• 0.007199932 / 0.00734687 = 98/100, check the degrees of freedom in the sem model. Commented Aug 1 at 10:49
• Thank you @Roland for noticing this! The df in the sem output is 0. Correct me if I am wrong, I am estimating three parameters (the β coefficient and variance of X and Y) with three pieces of information (variance of X and Y and covariance between X and Y). I think I am getting something with your response since 0.007199932 / 0.00734687 = 98/100 and the numerator is the df in the lm model while the denominator is the sample size. I am still confused why knowing the df in the sem model is helpful and why are they different. Thank you so much! Commented Aug 1 at 20:56