OLS models can be created using the base R lm function.

They can also be created using the ols function in the rms package.

Unfortunately, I cannot find anywhere online (including in the ols help page) a summary of the differences between ols and lm, and what reasons there might be in practice to prefer one or the other in particular circumstances.

(I assume that there are substantial differences, otherwise why should the authors of the rms package have created the ols function.)

A similar question arises with the competing mixed model functions in the lme4 and nlme packages, amongst (I suspect) many others.

There does not seem to be anywhere on the CRAN site where comparisons of the features of packages with overlapping coverage can be found. Are such comparisons available elsewhere online?

  • 4
    $\begingroup$ Can you explain how the first few sentences of ?ols do not explicitly answer your question? $\endgroup$ – joran Jul 12 '13 at 2:35
  • 1
    $\begingroup$ [Repeat of comment just added - which didn't come through] The ols help page does not explain to the non-R-expert what the practical significance of the differences is. Searching over at Cross Validated, there is only one answered question that includes both 'ols' and 'rms'. Clearly, the ols function is not uppermost in answerers' minds when dealing with OLS queries (of which there are, of course, many). So the question remains: what does ols offer in practice that lm does not? $\endgroup$ – DavidP Jul 12 '13 at 2:53
  • 7
    $\begingroup$ Explaining the documentation to you in this case really amounts to explaining the statistics to you, which is off topic for StackOverflow. R's documentation is never the right tool to educate yourself about statistics. For that you'll need to turn elsewhere. $\endgroup$ – joran Jul 12 '13 at 2:59
  • 3
    $\begingroup$ The question remains, ... but it also remains off-topic for SO. You should buy Harrell's RMS text and learn to use the interconnected facilities offered by the package. The practical advantage is that it is part of system. $\endgroup$ – DWin Jul 12 '13 at 6:43

I'm not quite getting the negative undertone to this conversation, but in brief there are five differences.

  1. ols implements penalized least squares (default = no penalty)
  2. The default contrast used for categorical predictors is reference cell indicator variable coding. If you don't have categorical predictors (or if you specify options(contrasts=...), regression coefficients and standard errors from ols are identical to lm.
  3. ols remembers much more about the data than lm so that graphics displaying the fitted model are easier to draw and certain estimates (e.g., inter-quartile-range difference in means) are easier to get
  4. rms implements a restricted interaction surface that ols can fit
  5. Being part of the rms package makes other things such as bootstrapping, model validation, comprehensive anova (e.g., tests of nonlinear interaction) easy to do, $\LaTeX$ typesetting of model fits, etc.
  • 1
    $\begingroup$ I think part of the negative undertone is from the default style of ? in R. I have seen R described as "expert friendly". This can put non-experts off. $\endgroup$ – Peter Flom - Reinstate Monica Jul 15 '13 at 21:24

I think part of the whole issue here is the nature of R vs. a system like SAS or SPSS or what have you. There are many differences - and many favor R. One key difference is that R is free. That means that the people who develop it are not paid to do so. So, while many of them try to be very helpful, they are writing packages because they think it's fun (or, in some cases, because it helps their careers - publish or perish).

SAS costs a great deal of money.

One thing you get for that money is people who are paid to do things, including things that are boring. So, for example, in the SAS documentation for most statistical PROCs there is an extensive section on comparisons with other PROCs. In addition, the SAS documentation has big sections on statistics (as opposed to the PROCs), the documentation lists many references, has many fully worked and annotated examples etc.

For example, if you type ?lm you get something that, if printed out, might be 5 pages or so (at a guess). The SAS documentation for PROC GLM (the rough equivalent) is well over 100 pages. The example at the end of ?lm is 12 lines long (admittedly, there are more complex examples elsewhere). The first example in PROC GLM is about 10 pages.

In addition, and compounding the above, is that R documentation almost prides itself on its terseness; while SAS documentation prides itself on verbosity and completeness.

I like both SAS and R. But they are very different in many ways, including what you can expect of the people who develop the packages; indeed, at SAS the people who develop the PROCs are usually not the people who write the DOCs; and they certainly aren't the people who answer the help lines. OTOH, here, you asked a question about rms and you got an answer from Frank Harrell; and you got it promptly, too.


Please check the handout of FE Harrell here at page 155.

as in Table 6.1

Function                 Purpose                      Related S Functions
ols           Ordinary least squares linear model           lm

Table shows that there is no difference between these two functions.

  • $\begingroup$ Dangerously incorrect to say there is no difference. related (ie using same underlying fitting function) doesn't mean no difference. $\endgroup$ – mnel Jul 12 '13 at 5:28
  • $\begingroup$ yeah, but that's something possible only after looking the syntax $\endgroup$ – Metrics Jul 12 '13 at 12:17
  • $\begingroup$ Doesn't seem to me unreasonable (your mileage seems to differ) to expect that a piece of freeware within a suite like R would be documented (and not in a way that requires payment, like buying the author's book - kinda negates the 'free' aspect!) so as to distinguish it from other software provided within the same suite. I'm not expecting the documentation for a particular OLS software to tell me how to do OLS: only to identify those elements which have a practical difference (compared with other software in the same suite) for those seeking to use R to implement an OLS model. (cont...) $\endgroup$ – DavidP Jul 12 '13 at 14:19
  • $\begingroup$ My understanding (you'll point out exactly how that's wrong if it is) is that Stack Overflow is for queries about the implementation of procedures by specific software. That's what my query is - the implementation of OLS models in R, by the ols compared with the lm functions. $\endgroup$ – DavidP Jul 12 '13 at 14:20
  • 1
    $\begingroup$ @DavidP Look at it the other way around: Frank Harrell wrote a (great) book (that's his job, he is a professor) and instead of being content with a high-level exposé, he also went to the trouble of developing software to implement the methods in his book. Would you rather have this software be a standalone package or only available to the book's buyers? It's not that it's not legitimate to wonder about the differences but it's in fact quite common for R packages to be paired with a book. Personnally, I prefer it that way than to read books with no implementations or only SAS macros. $\endgroup$ – Gala Jul 15 '13 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.