Re: [R] se's and CI's for fitted lines in multivariate regression analysis
On Oct 16, 2012, at 11:58 AM, Sigrid wrote: > 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.53570.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. The SE's for treatment "slope" will vary depending on the colpos values. Using `predict`, pick the mid-point of the colpos and rowpos variables (which is where the SE of the estimates will be minimized). This should be covered in any basic regression text. model<-lm(decrease ~ rowpos + colpos + treatment + treatment:colpos + 0, data=OrchardSprays) # I do not think the use of the non-intercept version matters here and it's not in general a good practice, but it allows all the parameters to be labeled as you apparently expect. predict(model, newdata=data.frame(colpos=4.5, treatment=unique(OrchardSprays$treatment), rowpos=mean(OrchardSprays$rowpos) ), se.fit = TRUE, interval = "confidence") $fit fitlwr upr 1 35.000 20.331646 49.66835 2 63.125 48.456646 77.79335 3 7.625 -7.043354 22.29335 4 90.250 75.581646 104.91835 5 68.500 53.831646 83.16835 6 69.000 54.331646 83.66835 7 25.250 10.581646 39.91835 8 4.625 -10.043354 19.29335 $se.fit 12345678 7.291375 7.291375 7.291375 7.291375 7.291375 7.291375 7.291375 7.291375 $df [1] 47 $residual.scale [1] 20.62312 > unique(OrchardSprays$treatment) [1] D E B H G F C A Levels: A B C D E F G H > with(OrchardSprays, tapply(decrease, treatment, mean) ) A B C D E F G H 4.625 7.625 25.250 35.000 63.125 69.000 68.500 90.250 -- David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] se's and CI's for fitted lines in multivariate regression analysis
Hi again, Thank you so much for the script. Unfortunately, I feel like I might not have explained things clearly enough from the start. What I’m looking for is the st. errors or CI intervals for the estimate the parameter for slope and intercept for each level of each factor. From the summary table I can find the intercept and slope for all treatments by adding them up. Here the printout from the summary table: (I’ll just do for level A and B to illustrate) Estimate Std. Error t value Pr(>|t|) (Intercept) 18.00299 17.43720 1.032 0.307146 rowpos-2.887231.26369 -2.285 0.026886 * colpos-0.085663.19131 -0.027 0.978699 treatmentB 1.22690 22.73203 0.054 0.957186 : colpos:treatmentB 0.394024.50194 0.088 0.930628 >From this I can find the lines of A and B by adding up the parameters A, intercept: 18.00299+(-2.88723)= *15.14267* A, slope: *-0.08566* B, intercept: 18.00299+(-2.88723)+1.22690)= *16.36957* B, slope: -0.08566+0.39402=-*0.46258* It would be nice if I could get the intercept and slope done for me, like this. lm(formula = decrease[treatment == "B"] ~ colpos[treatment == "B"]) Coefficients: (Intercept) colpos[treatment == "B"] 5.0.5833 but while I do that with my more complicated model, I get an error message. >lm(decrease[treatment=="B"]~rowpos[treatment=="B"]+colpos[treatment=="B"]+treatment[treatment=="B"]+treatment:colpos[treatment=="B"]) Error in model.frame.default(formula = decrease[treatment == "B"] ~ rowpos[treatment == : variable lengths differ (found for 'treatment') So back to the actual problem; how do I get the standard error of intercept and slope or CI of those lines (A&B) calculated above? As the coefficients given in the model are the difference between each factor from ‘A’ it does not make sense to add them up in the same way as the parameters themselves -- View this message in context: http://r.789695.n4.nabble.com/se-s-and-CI-s-for-fitted-lines-in-multivariate-regression-analysis-tp4645703p4646878.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] se's and CI's for fitted lines in multivariate regression analysis
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, Rui Barradas 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[1] 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. Rui Barradas 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.53570.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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] se's and CI's for fitted lines in multivariate regression analysis
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[1] 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. Rui Barradas 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.53570.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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] se's and CI's for fitted lines in multivariate regression analysis
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.53570.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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] se's and CI's for fitted lines in multivariate regression analysis
Hello, Your model is equivalent to the model below. As for standard errors, try predict.lm with the appropriate argument. ?predict.lm model <- lm(decrease ~ rowpos + colpos*treatment, data = OrchardSprays) predict(model, se.fit = TRUE, interval = "confidence") Hope this helps, Rui Barradas Em 10-10-2012 14:49, Sigrid escreveu: I’m entirely stumped on this particular issue and really hoping someone has some advice for me. I am running a covariant model in lm I would like to give the standard errors or the confidence intervals for the fitted lines. I’ve been using the dataset OrangeSprays where I want lines for each level of treatment over the covariant ‘colpos’. I’ve been able to calculate intercepts and slopes for each lines by adding up the parameters, but this does not make sense when the comes to the se’s. I’ve run the summary (model) function, but only get the differences in standard error between lines. Also by running confint(model), I have the same problem. How can I get standard error per or the CI-interval for each fitted line? I’ve attached the coding underneath. Thank you, Sigrid data(OrchardSprays) model<-lm(decrease~rowpos+colpos+treatment+treatment:colpos) summary(model) -- View this message in context: http://r.789695.n4.nabble.com/se-s-and-CI-s-for-fitted-lines-in-multivariate-regression-analysis-tp4645703.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] se's and CI's for fitted lines in multivariate regression analysis
Hi Bert, I just looked at An Introduction to R - and I do apologize if my questions are trivial. I see that they use predict as a function in lm, but I'm not sure how to incorporate it into a command. 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-tp4645703p4645715.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] se's and CI's for fitted lines in multivariate regression analysis
?predict Have you read An Inrtroduction to R? -- Bert On Wed, Oct 10, 2012 at 6:49 AM, Sigrid wrote: > I’m entirely stumped on this particular issue and really hoping someone has > some advice for me. > > I am running a covariant model in lm I would like to give the standard > errors or the confidence intervals for the fitted lines. I’ve been using the > dataset OrangeSprays where I want lines for each level of treatment over the > covariant ‘colpos’. I’ve been able to calculate intercepts and slopes for > each lines by adding up the parameters, but this does not make sense when > the comes to the se’s. I’ve run the summary (model) function, but only get > the differences in standard error between lines. Also by running > confint(model), I have the same problem. > > How can I get standard error per or the CI-interval for each fitted line? > I’ve attached the coding underneath. > > Thank you, > Sigrid > >> data(OrchardSprays) >> model<-lm(decrease~rowpos+colpos+treatment+treatment:colpos) >> summary(model) > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/se-s-and-CI-s-for-fitted-lines-in-multivariate-regression-analysis-tp4645703.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.