To fit a fixed effect model in R, we typically use the lm()
function. A fixed effect model is often used in panel data analysis where we have data over time on a set of individuals or entities (e.g., countries, firms, people).
Assume we have panel data on n
individuals over T
time periods with dependent variable y
, an independent variable of interest x
, and a set of K
control variables z1, z2, ..., zK
. The id
variable in the dataset represents the individual identifier, while the time
variable represents the time period identifier.
To fit the fixed effect model, we first create a dataset containing individual-specific deviations from the time-invariant means of the variables. We then run the linear regression using the lm()
function as follows:
main.r172 chars6 lines
The factor(id)
term includes a fixed effect for each individual. We can estimate the fixed effect model using the plm()
function from the plm
package in the same way as above. However, plm()
provides more relevant output (such as estimation of models with serial correlation or adjusting for heteroscedasticity), so it's better for panel data analysis in general.
main.r204 chars6 lines
gistlibby LogSnag