简体   繁体   中英

Confidence Interval after linear regression model in R

I am trying to get the confidence interval after fitting the linear regression model.
My data contains:

Growthrate  Strains
<dbl>       <fct>

where growthrate represents growthrate value and Strains represents strain names

my code:

all.lm <- lm(Growthrate~Strains,all) 

confint(all.lm, 'Strains',level=0.95)

Output:

          2.5 % 97.5 %  
Strains    NA     NA

I don't understand why its printing NA. Any help at this point is highly appreciated.

The problem is that Strains is a categorical variable so there is no coefficient called Strains . Here is an example using the iris data set that is included with R:

data(iris)
iris.lm <- lm(Petal.Width~Species, iris)
confint(iris.lm, level=.95)   # All coefficients
#                       2.5 %    97.5 %
# (Intercept)       0.1888041 0.3031959
# Speciesversicolor 0.9991128 1.1608872
# Speciesvirginica  1.6991128 1.8608872

confint(iris.lm, "Species", level=.95)  # Species is not a coefficient
#         2.5 % 97.5 %
# Species    NA     NA

You need to specify the coefficients by name or number:

confint(iris.lm, 2:3, level=.95)
#                       2.5 %   97.5 %
# Speciesversicolor 0.9991128 1.160887
# Speciesvirginica  1.6991128 1.860887

cfnames <- names(coef(iris.lm))[2:3]
cfnames
# [1] "Speciesversicolor" "Speciesvirginica" 
confint(iris.lm, cfnames, level=.95)
#                       2.5 %   97.5 %
# Speciesversicolor 0.9991128 1.160887
# Speciesvirginica  1.6991128 1.860887

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM