1
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ Can you describe what predict uhat, residuals does? $\endgroup$ – Jeremy Miles Mar 8 '15 at 23:16
  • $\begingroup$ 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? $\endgroup$ – Matifou Mar 9 '15 at 0:01
  • $\begingroup$ 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. $\endgroup$ – Nick Cox Mar 9 '15 at 0:39
1
$\begingroup$

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)
$\endgroup$
  • 1
    $\begingroup$ +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. $\endgroup$ – Glen_b -Reinstate Monica Mar 9 '15 at 4:31

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.