Solved – Confidence interval for values for a fitted line

confidence intervaljmpr

I'm using JMP to analyze some sample data to make predictions about the population. My sample is from a destructive QC test, so I obviously want to minimize my sample. I have a response (my Y) and a known factor (a very strong and consistent correlation that is measurable by non-destructive means) but the exact relationship between them varies from lot to lot (the slope and y offset vary).

So, in JMP, I am fitting a line and then showing the "confidence limits for an individual predicted value" which I believe gives me an indicator of how the population is likely to behave. So I'm using that plot to make disposition decisions. I want to automate this process, perhaps using R, but I'm a total novice at R. I could do the math if I was just dealing with a mean and standard deviation, but I don't know how to do it with a fit line and a known factor. Can someone please give me either the general information on how to get the confidence limits around the line, or else tell me how to do the whole thing in R?

Thankss much.

Best Answer

If your using linear regression I would recommend using the rms package in R. It is very easy to use and has lots of nice features.

Here's an example:

# Load package (remember to install.packages("rms") or this will fail the first time)
library(rms)

# Get a dataset to experiment with
data(mtcars)
mtcars$am <- factor(mtcars$am, levels=0:1, labels=c("Automatic", "Manual"))

# The rms package needs this work properly
dd <- datadist(mtcars)
options(datadist="dd")

# Do the regression
f <- ols(mpg~wt, data=mtcars, x=T, y=T)

# Plot regular mean confidence interval
p <- Predict(f, wt=seq(2.5, 4, by=.001), conf.type="mean")
plot(p, ylim=c(10, 30), col="lightblue")

# Plot wide confidence interval
p <- Predict(f, wt=seq(2.5, 4, by=.001), conf.type="individual")
plot(p, ylim=c(10, 30), col="lightblue")

Gives this output:

Plain line

Now usually you want to test the linearity assumption:

# Try the model with a restricted cubic spline
f <- ols(mpg~rcs(wt, 3), data=mtcars, x=T, y=T)
anova(f)

Gives this output:

> anova(f)
                Analysis of Variance          Response: mpg 

 Factor     d.f. Partial SS MS         F     P     
 wt          2   922.04230  461.021149 65.54 <.0001
  Nonlinear  1    74.31705   74.317047 10.56 0.0029
 REGRESSION  2   922.04230  461.021149 65.54 <.0001
 ERROR      29   204.00489    7.034651             

And if you plot the graphs with the same code as a bove you get this picture:

Regression with a spline

If you want to make your formula more complicated just add that variable:

f <- ols(mpg~rcs(wt, 3)+am, data=mtcars, x=T, y=T)
p <- Predict(f, wt=seq(2.5, 4, by=.001), am=levels(mtcars$am), conf.type="individual")
plot(p)

I don't know anything about JMP, it shouldn't be too difficult but I recommend learning R because it gives you an incredible freedom.

Hope this helped.