Techno Blender
Digitally Yours.

Plot Simultaneous and Pointwise Confidence Bands for Linear Regression

0 36


library(dplyr)

setosa <- iris %>% filter(Species == "setosa")

                %>% select(Sepal.Length,

                           Sepal.Width, 

                           Species)

virginica <- iris %>% filter(Species == "virginica")

                    %>% select(Sepal.Length, 

                               Sepal.Width,

                               Species)

  

setosa <- setosa %>% arrange(Sepal.Length)

virginica <- virginica %>% arrange(Sepal.Length)

  

 compute linear models

Model <- as.formula(Sepal.Width ~ poly(Sepal.Length,2))

fit1  <- lm(Model, data = setosa)

fit2  <- lm(Model, data = virginica)

X1  <- model.matrix(fit1)

X2  <- model.matrix(fit2)

cht1 <- multcomp::glht(fit1, linfct = X1) 

cht2 <- multcomp::glht(fit2, linfct = X2) 

cc1 <- confint(cht1); cc1 <- as.data.frame(cc1$confint)

cc2 <- confint(cht2); cc2 <- as.data.frame(cc2$confint)

setosa$LowerBound <- cc1$lwr

setosa$UpperBound <- cc1$upr

virginica$LowerBound <- cc2$lwr

virginica$UpperBound <- cc1$upr

  

mydata <- rbind(setosa, virginica)

  

library(ggplot2)

ggplot(data = mydata, aes(x = Sepal.Length,

                          y = Sepal.Width, 

                          color = Species)) + 

  

  geom_point() +

  

  

  geom_smooth(method ="lm", formula = y ~ poly(x,2)) +

  

  geom_ribbon(aes(ymin = LowerBound,

                  ymax = UpperBound),

              alpha = 0.5, 

              fill = "grey70")


library(dplyr)

setosa <- iris %>% filter(Species == "setosa")

                %>% select(Sepal.Length,

                           Sepal.Width, 

                           Species)

virginica <- iris %>% filter(Species == "virginica")

                    %>% select(Sepal.Length, 

                               Sepal.Width,

                               Species)

  

setosa <- setosa %>% arrange(Sepal.Length)

virginica <- virginica %>% arrange(Sepal.Length)

  

 compute linear models

Model <- as.formula(Sepal.Width ~ poly(Sepal.Length,2))

fit1  <- lm(Model, data = setosa)

fit2  <- lm(Model, data = virginica)

X1  <- model.matrix(fit1)

X2  <- model.matrix(fit2)

cht1 <- multcomp::glht(fit1, linfct = X1) 

cht2 <- multcomp::glht(fit2, linfct = X2) 

cc1 <- confint(cht1); cc1 <- as.data.frame(cc1$confint)

cc2 <- confint(cht2); cc2 <- as.data.frame(cc2$confint)

setosa$LowerBound <- cc1$lwr

setosa$UpperBound <- cc1$upr

virginica$LowerBound <- cc2$lwr

virginica$UpperBound <- cc1$upr

  

mydata <- rbind(setosa, virginica)

  

library(ggplot2)

ggplot(data = mydata, aes(x = Sepal.Length,

                          y = Sepal.Width, 

                          color = Species)) + 

  

  geom_point() +

  

  

  geom_smooth(method ="lm", formula = y ~ poly(x,2)) +

  

  geom_ribbon(aes(ymin = LowerBound,

                  ymax = UpperBound),

              alpha = 0.5, 

              fill = "grey70")

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment