# [R] se's and CI's for fitted lines in multivariate regression analysis

Wed Oct 17 11:49:13 CEST 2012

```Hello,

Thinking again, there are better ways of getting all the CI's for the
fitted lines in one go. First, make a list of models, subsetting on each
level of treatment. Then lapply predict.lm to the list of models.

levs <- levels(OrchardSprays\$treatment)
model.lst <- lapply(levs, function(.lvl)
lm(decrease ~ colpos , subset = treatment == .lvl, data =
OrchardSprays))

pred.lst <- lapply(model.lst, predict, interval = "confidence") # Get
all CI's

names(model.lst) <- names(pred.lst) <- levs

pred.lst\$A  # Just level 'A'

Hope this helps,

Em 16-10-2012 22:54, Rui Barradas escreveu:
> Hello,
>
> If you want confidence intervals for the beta coefficients of the
> model, try the following.
>
> ci_lm <- function(object, level = 0.95){
>     sfit <- summary(object)
>     beta <- sfit\$coefficients[, 1]
>     se <- sfit\$coefficients[, 2]
>     df <- sfit\$df
>     alpha <- 1 - level
>     lower <- beta + qt(alpha/2, df = df)*se
>     upper <- beta + qt(1 - alpha/2, df = df)*se
>     data.frame(beta, lower, upper)
> }
>
> data(OrchardSprays)
> model <- lm(decrease ~ rowpos + colpos * treatment, data = OrchardSprays)
> ci_lm(model)
>
>
> On the other hand, if you want to run regressions on each factor level
> separately, use the argument 'subset' of lm().
>
> model2 <- lm(decrease ~ colpos , subset = treatment == 'A', data =
> OrchardSprays)
> model2
>
> I believe that you might be looking for this last one.
>
> Em 16-10-2012 19:58, Sigrid escreveu:
>> Okay, I've now tried to the predict function and get the SE, although
>> it seem
>> to calculate SE for each observation from the line (I assume), while
>> I want
>> the CI-interval and SE for each line fitted line for the treatment. I
>> do not
>> really understand what  parameter mean these SEs are calculated from
>> when
>> there would be several means along the line...?. This is what I get with
>> predict:
>>
>>> predict(model, se.fit = TRUE, interval = "confidence")
>> Another way I can think of to show how well the lines fit the data is to
>> look at the intercepts and slopes instead. I can specify the line for
>> each
>> level and would then get the estimate of slope and intercept,
>> although I do
>> not know how I show the standard errors of the slope and intercept.
>> lm(decrease[treatment=="A"]~colpos[treatment=="A"])
>>
>> Call:
>> lm(formula = decrease[treatment == "A"] ~ colpos[treatment == "A"])
>>
>> Coefficients:
>>               (Intercept)  colpos[treatment == "A"]
>>                    2.5357                    0.4643
>>
>> Please let me know if you know how to find st. errors for (or st.
>> error for
>> slope and intercept) of lines for each factor of a treatment.
>>
>> Thank you
>> ~S
>>
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/se-s-and-CI-s-for-fitted-lines-in-multivariate-regression-analysis-tp4645703p4646393.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help