I am trying to replicate a linear regression model in R that was run in SAS

when I run the following model lm(y~A*B, data=ex2) (where A and B are categorical variables) and compare it to the output from SAS (using a type III Anova and the same referent levels), I get the same least squared means, CIs and Anova output to the decimal, however my co-efficients are off and I have smaller SEs in R. Since SAS uses ML and R uses OLS I am thinking this may be the reason, am I able to change the default of lm to ML? Any other suggestions? Thanks. CB

• If the errors are assumed to belong to a normal distribution, MLE and least squares are equivalent and should lead to the exact same estimates. – COOLSerdash Apr 12 '19 at 17:25
• @COOL Aren't the MLE estimates of variance always smaller than the OLS ones (which are unbiased)? As far as the coefficients go, it is tempting to begin the investigation by comparing how the two applications code the categories. – whuber Apr 12 '19 at 18:12
• @whuber Good point, I actually forgot about the estimate of the residual variance and the difference between OLS and MLE there. Thanks for noticing. – COOLSerdash Apr 12 '19 at 20:56
• What proc are you using in SAS? Why not provide a reproducible example? – Sal Mangiafico Apr 13 '19 at 21:43
• Here is the data frame I am using: NDS<-matrix(c(1,1,1,10.54,1,1,2,10.24,1,1,3,11.67,1,3,1,8.64, + 1,3,2,8.02,1,3,3,9.56,1,4,1,6.7,1,4,2,8.03,1,4,3,9.8, + 2,1,1,11.97,2,1,2,11.36,2,1,3,11.92,2,2,1,12.4,2,2,2,12.31, + 2,2,3,12.91,2,3,1,9.37,2,3,2,10.65,2,3,3,9.37,3,1,1,12.93, + 3,1,2,13.91,3,1,3,11.83,3,2,1,11.39,3,2,2,12.06,3,2,3,12.3, + 3,3,1,12.88,3,3,2,11.6,3,3,3,12.84,3,4,1,13.35,3,4,2,9.8, + 3,4,3,13.37,1,2,1,8.78,2,4,2,6.81),nrow=32, ncol=4, byrow=T) > colnames(NDS)<-c("A","B","rep","y") – CB55 Apr 15 '19 at 17:08