-1

Given a regression model:

y = b0 + b1(x)

where both x and y are continuous.

After fitting the model, I'd like to estimate the predicted mean and 95%CI of y when x is at a certain value, say, 100.

In Stata, it can be achieved with margins:

reg y x
margins, at (x = 100)

In SAS, it can be done with estimate:

proc glm;
model y = x / clparm solution;
estimate "Test x = 100" intercept 1 x 100;
run;

My question is: how to achieve the same action in R? I tried the lsmeans package but it seems it will not work if I don't have any categorical variables in my model.

Penguin_Knight
  • 1,289
  • 8
  • 13

1 Answers1

1
predict(fit,newdata=data.frame(x=100),interval="confidence")

(I don't agree with @thelatemail's advice that prediction intervals are preferred; prediction intervals are specified if you want to allow for the residual error ...)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thank you! That's very helpful. – Penguin_Knight Oct 29 '15 at 13:14
  • 1
    Technically, the R equivalent of `margins, at (x = 100)` would be `predict(fit, newdata = within(data, x = 100), interval="confidence")` as `margins` gives an average prediction across all cases with `x` fixed to the specified value, not a prediction for a single, specific case. – Thomas Aug 01 '18 at 21:03
  • Hmm. Is there a difference in the **univariate** case that the OP is asking about? @rvl, should there be a new/updated answer that references new functionality/syntax in `emmeans` ? – Ben Bolker Aug 01 '18 at 23:26