predict.fixest | R Documentation |
fixest
fitsThis function obtains prediction from a fitted model estimated with femlm
,
feols
or feglm
.
## S3 method for class 'fixest'
predict(
object,
newdata,
type = c("response", "link"),
se.fit = FALSE,
interval = "none",
level = 0.95,
fixef = FALSE,
vs.coef = FALSE,
sample = c("estimation", "original"),
vcov = NULL,
ssc = NULL,
...
)
object |
A |
newdata |
A data.frame containing the variables used to make the prediction.
If not provided, the fitted expected (or linear if |
type |
Character either equal to |
se.fit |
Logical, default is |
interval |
Either "none" (default), "confidence" or "prediction". What type of confidence interval to compute. Note that this feature is only available for OLS models not containing fixed-effects (GLM/ML models are not covered). |
level |
A numeric scalar in between 0.5 and 1, defaults to 0.95. Only used when the argument 'interval' is requested, it corresponds to the width of the confidence interval. |
fixef |
Logical scalar, default is |
vs.coef |
Logical scalar, default is |
sample |
Either "estimation" (default) or "original". This argument is only used when arg. 'newdata' is missing, and is ignored otherwise. If equal to "estimation", the vector returned matches the sample used for the estimation. If equal to "original", it matches the original data set (the observations not used for the estimation being filled with NAs). |
vcov |
Versatile argument to specify the VCOV. In general, it is either a character
scalar equal to a VCOV type, either a formula of the form: |
ssc |
An object of class |
... |
Not currently used. |
It returns a numeric vector of length equal to the number of observations in argument newdata
.
If newdata
is missing, it returns a vector of the same length as the estimation sample,
except if sample = "original"
, in which case the length of the vector will match the one
of the original data set (which can, but also cannot, be the estimation sample).
If fixef = TRUE
, a data.frame
is returned.
If se.fit = TRUE
or interval != "none"
, the object returned is a data.frame
with the following columns: fit
, se.fit
, and, if CIs are requested, ci_low
and ci_high
.
Laurent Berge
See also the main estimation functions femlm
, feols
or feglm
. update.fixest
, summary.fixest
, vcov.fixest
, fixef.fixest
.
# Estimation on iris data
res = fepois(Sepal.Length ~ Petal.Length | Species, iris)
# what would be the prediction if the data was all setosa?
newdata = data.frame(Petal.Length = iris$Petal.Length, Species = "setosa")
pred_setosa = predict(res, newdata = newdata)
# Let's look at it graphically
plot(c(1, 7), c(3, 11), type = "n", xlab = "Petal.Length",
ylab = "Sepal.Length")
newdata = iris[order(iris$Petal.Length), ]
newdata$Species = "setosa"
lines(newdata$Petal.Length, predict(res, newdata))
# versicolor
newdata$Species = "versicolor"
lines(newdata$Petal.Length, predict(res, newdata), col=2)
# virginica
newdata$Species = "virginica"
lines(newdata$Petal.Length, predict(res, newdata), col=3)
# The original data
points(iris$Petal.Length, iris$Sepal.Length, col = iris$Species, pch = 18)
legend("topleft", lty = 1, col = 1:3, legend = levels(iris$Species))
#
# Getting the fixed-effect coefficients for each obs.
#
data(trade)
est_trade = fepois(Euros ~ log(dist_km) | Destination^Product +
Origin^Product + Year, trade)
obs_fe = predict(est_trade, fixef = TRUE)
head(obs_fe)
# can we check we get the right sum of fixed-effects
head(cbind(rowSums(obs_fe), est_trade$sumFE))
#
# Standard-error of the prediction
#
base = setNames(iris, c("y", "x1", "x2", "x3", "species"))
est = feols(y ~ x1 + species, base)
head(predict(est, se.fit = TRUE))
# regular confidence interval
head(predict(est, interval = "conf"))
# adding the residual to the CI
head(predict(est, interval = "predi"))
# You can change the type of SE on the fly
head(predict(est, interval = "conf", vcov = ~species))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.