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.
CodePudding user response:
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
Actually (Intercept)
in this example is the coefficient for Speciessetosa
, the first factor level, so it would make sense to include this coefficient as well.