# Stata's predict uhat, residuals function in R

I'm having trouble figuring out how to replicate Stata's command "predict uhat, residuals" in R for creating residuals.

Do I have to generate a normal sampling to accomplish this?

Thank you.

• Can you describe what predict uhat, residuals does? – Jeremy Miles Mar 8 '15 at 23:16
• seems you just want to use residuals(), though your reference to normal sampling seems to indicate you are working with a specific (survey sampling?) model? – Matifou Mar 9 '15 at 0:01
• In Stata, predict can do different things depending on the previous model-fitting commands. But with the option residuals it is usually calculating plain residuals. – Nick Cox Mar 9 '15 at 0:39

Not entirely sure about the "Do I have to generate a normal sampling to accomplish this?" but here are the parallel examples:

In Stata the predict command will not work unless you have done some analysis before that. For example, linear regression using reg command.

sysuse auto
reg price mpg
predict uhat, residual


This will give you the residual called uhat.

In R, same idea. You'll need to have an object first. After the lm() command, a set of residual will be saved in the model output. You'll need to use $residual to get it. Again here is an example in linear regression: x <- rnorm(1000) y <- x * 1.5 + rnorm(1000, 0, 250) m01 <- lm(y ~ x) uhat <- m01$residual
hist(uhat)

• +1 Or residuals(m01), which at least one advantage - it works with several model objects that don't come with a \$residuals component already in them. – Glen_b -Reinstate Monica Mar 9 '15 at 4:31