This might be an r question, which I think is not allowed here, but I figured I would try in case it is ok. There are a series of tests which as I understand it rely on residuals. One example of this is the Breusch-Pagan test. This generates apparent results from that (and the documentation seems to require a regression model rather than residuals).

# load the dataset
# fit a regression model
model <- lm(mpg~disp+hp, data=mtcars)
# perform Breusch-Pagan Test

Is the code extracting the residuals or am I wrong in assuming you need residuals for this test (the comments I read seem pretty clear it is run on the residuals).

  • $\begingroup$ Yes, the test uses residuals and the function - which you can inspect by writing bptest in R - extracts the residuals for us. Related: stats.stackexchange.com/questions/468778/… $\endgroup$ Aug 25, 2020 at 15:42
  • $\begingroup$ Thanks. Thank you also for editing it, I did not realize [code][/code] did not work (or what does work for code). Is it true that questions about R are generally not allowed here? I ask because I have found many R questions on this board in years past (I am new to R and learn a lot about it from past posts on this forum). $\endgroup$
    – user54285
    Aug 25, 2020 at 17:20
  • $\begingroup$ On R questions: this guidance is at stats.stackexchange.com/help/on-topic "If the language is statistically oriented (such as R, SAS, Stata, SPSS, etc.), then decide based on the nature of your question: if it needs statistical expertise to understand or answer, ask it here; if it's about the implementation of an algorithm, routine data processing, or details of the language, then please refer to the collection of links to resources we maintain." $\endgroup$
    – Nick Cox
    Aug 26, 2020 at 7:36

1 Answer 1


To add to @ChristophHanck's comment, you can check the source code for bptest and in it you see that a formula and a data frame can be applied:

bptest(mpg~disp+hp, data=mtcars)

studentized Breusch-Pagan test

data:  mpg ~ disp + hp
BP = 4.0861, df = 2, p-value = 0.1296

Underneath the code, there's two parts. If you provide a lm object, it will extract the x and y, refit the formula using lm.fit. After which, it extracts the residuals from the lm.fit object and calculates the relevant statistics. These are the relevant lines:

resi <- lm.fit(X,y)$residuals
  sigma2 <- sum(resi^2)/n

    w <- resi^2 - sigma2
    aux <- lm.fit(Z, w)    
    bp <- n * sum(aux$fitted.values^2)/sum(w^2)
    method <- "studentized Breusch-Pagan test"
    f <- resi^2/sigma2 -1
    aux <- lm.fit(Z, f)
    bp <- 0.5 * sum(aux$fitted.values^2)
    method <- "Breusch-Pagan test"

  names(bp) <- "BP"
  df <- c("df" = aux$rank - 1)
  RVAL <- list(statistic = bp,
      parameter = df,
      method = method,
      p.value= pchisq(bp, df, lower.tail = FALSE),
      data.name = dname)
  • $\begingroup$ Thanks very much.Now that I know that, I really am new to R being a SAS user all my life, I started to look at other tests that I had questions on such as Breusch-Godfrey serial correlation. I don't understand a lot of the code, but I can see where it pulls residuals. What language is R written in, I need to try to understand better what lies behind it. is it possible to find the algorithms that lie behind the code as well? When results vary from SAS I (or my team that is also learning R) need to understand why. $\endgroup$
    – user54285
    Aug 25, 2020 at 17:33
  • $\begingroup$ R is a programming language, en.wikipedia.org/wiki/R_(programming_language) . Some of the base functions would call a C or fortran code.. It's is a good question what is the algorithm behind the code..normally in the manual or vignette the authors will indicate where the algo comes from, for example this bptest,cran.r-project.org/web/packages/lmtest/lmtest.pdf $\endgroup$
    – StupidWolf
    Aug 25, 2020 at 17:39
  • $\begingroup$ Using this example, bptest, it extracts the residuals (resi), finds the mean of the residuals (sigma2) and then performs the auxillary regression.. So I guess if you have a clue on how it should be implemented, and have a knowledge of what each function does, it will make sense $\endgroup$
    – StupidWolf
    Aug 25, 2020 at 17:44
  • $\begingroup$ @user54285 This book might be helpful to make the transition from SAS to R easier: springer.com/gp/book/9781461406846 $\endgroup$
    – Stefan
    Aug 25, 2020 at 17:50
  • $\begingroup$ thanks. I have a group of programmers on my team (I am the one who knows the statistics and figures out what we need to do). I will try to learn it, but I am sure they will learn it far faster.:) I really appreciate the help. $\endgroup$
    – user54285
    Aug 25, 2020 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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