I am trying to reproduce with R the results from Intercepts- and Slopes-as Outcomes model from Hierarchical Linear Models by Raudenbush and Bryk, which deals with High School and Beyond dataset.
Model and results from Raudenbush book
The hsb dataset comes from the mlmhelpr package and my R code is:
library(lme4)
library(mlmhelpr)
hsb=hsb
groupMeanses = aggregate(hsb$ses, list(hsb$id), FUN = mean, data=hsb)
names(groupMeanses) = c('id','groupMeanses')
hsb = merge(hsb, groupMeanses, by = c('id'))
hsb$ses_centered_a = (hsb$ses - hsb$groupMeanses)
hsb$ses_centered_b = (hsb$ses - hsb$meanses)
M3d = lmer(mathach ~ 1 + ses_centered_b * meanses + ses_centered_b * catholic
+ (1+meanses|id) , REML = F, data=hsb,
control = lmerControl(optimizer = "bobyqa", boundary.tol = 1e-5))
summary(M3d)
and the results from it:
As you can see, both results (those from Raudenbush and Bryk book and my R code) look pretty close, except for the random effects. I found a tutorial on Internet that fits the same model using HLM 6 that reproduces the results correctly, so I must be doing something wrong in R.
My question is: what modification should I do in my code or what is the right way in R to get the right results?

It's hard to be absolutely sure, but my best guess is that the various packages in R (I tried several; see below) are coming up with better fits to the data than HLM6. In particular, the HLM6 output you list reports a deviance (in this case, -2 times the log-likelihood) of 46501.875643; several different R packages report deviances of 46497.4 (about 4.4 deviance units lower). Lower deviances are better, so it seems that R's fits are actually better.
In a little more detail: I fitted the same model with
lme4::lmer,nlme::lme, andglmmTMB::glmmTMB, which all implement mixed model fitting in (slightly) different ways. They all found singular fits, with small or minimal slope standard deviations (from 6e-6 to 0.05), highly variable correlation between slopes and intercepts (which makes sense — as the slope SD goes to zero, the slope-intercept correlation becomes undefined). The deviances and estimates of intercept SD and residual SD were highly consistent.I also used the
allFit()function to try a wide range of optimizers withinlmer- these all gave consistent answers.The next test would be to take the estimates from HLM6, evaluate the log-likelihoods from the various R packages (this is fairly straightforward with
lmerandglmmTMB), and see if we recover the HLM6 deviance estimate. Unfortunately, HLM6 doesn't appear to report the intercept-slope covariance (or correlation).