library(plm)
## Warning: package 'plm' was built under R version 4.4.1
# Load the Grunfeld dataset
data("Grunfeld")
str(Grunfeld)
## 'data.frame': 200 obs. of 5 variables:
## $ firm : int 1 1 1 1 1 1 1 1 1 1 ...
## $ year : int 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 ...
## $ inv : num 318 392 411 258 331 ...
## $ value : num 3078 4662 5387 2792 4313 ...
## $ capital: num 2.8 52.6 156.9 209.2 203.4 ...
# Load the required package
library(plm)
# Load the Grunfeld dataset
data("Grunfeld", package = "plm")
# Check if the dataset is balanced
is_balanced <- plm::is.pbalanced(Grunfeld)
if(is_balanced) {
print("The Grunfeld dataset is balanced.")
} else {
print("The Grunfeld dataset is not balanced.")
}
## [1] "The Grunfeld dataset is balanced."
# Identify the time component in the dataset
time_component <- unique(Grunfeld$year)
print(paste("Time component:", time_component))
## [1] "Time component: 1935" "Time component: 1936" "Time component: 1937"
## [4] "Time component: 1938" "Time component: 1939" "Time component: 1940"
## [7] "Time component: 1941" "Time component: 1942" "Time component: 1943"
## [10] "Time component: 1944" "Time component: 1945" "Time component: 1946"
## [13] "Time component: 1947" "Time component: 1948" "Time component: 1949"
## [16] "Time component: 1950" "Time component: 1951" "Time component: 1952"
## [19] "Time component: 1953" "Time component: 1954"
# Identify the entity component in the dataset
entity_component <- unique(Grunfeld$firm)
print(paste("Entity component:", entity_component))
## [1] "Entity component: 1" "Entity component: 2" "Entity component: 3"
## [4] "Entity component: 4" "Entity component: 5" "Entity component: 6"
## [7] "Entity component: 7" "Entity component: 8" "Entity component: 9"
## [10] "Entity component: 10"
In the Grunfeld dataset, the time component and entity component can be identified as follows:
Time Component: The time component in the Grunfeld dataset represents the unique years for which data is available. You can extract the unique years from the dataset to identify the time component.
Entity Component: The entity component in the Grunfeld dataset represents the unique entities or firms for which data is recorded. You can extract the unique firm identifiers from the dataset to identify the entity component.
Ordinary Least Squares (OLS) regression on the Grunfeld dataset in R. Defining a meaningful estimating equation. Let’s consider estimating the investment equation using the variables inv (investment), value (market value of the firm), and capital (real capital stock). The estimating equation can be formulated as:
inv i =β0 + β1valuei +β2.capitali + ui
Where: invi is the investment for firm i. valuei is the market value of the firm for firm i. capital i is the real capital stock for firm i. ui is the error term.
# Load the required package
library(plm)
# Load the Grunfeld dataset
data("Grunfeld", package = "plm")
# Define the estimating equation
model <- lm(inv ~ value + capital, data = Grunfeld)
# Run the OLS regression and print the results
summary(model)
##
## Call:
## lm(formula = inv ~ value + capital, data = Grunfeld)
##
## Residuals:
## Min 1Q Median 3Q Max
## -291.68 -30.01 5.30 34.83 369.45
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -42.714369 9.511676 -4.491 1.21e-05 ***
## value 0.115562 0.005836 19.803 < 2e-16 ***
## capital 0.230678 0.025476 9.055 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 94.41 on 197 degrees of freedom
## Multiple R-squared: 0.8124, Adjusted R-squared: 0.8105
## F-statistic: 426.6 on 2 and 197 DF, p-value: < 2.2e-16
Below is the explanation of the estimated coefficients of the direction, magnitude, statistical significance and the interpretation with the output:
Direction: Look at the signs of the coefficients for Value and Capital. If the coefficient for Value is positive, it suggests that an increase in the firm’s value is associated with an increase in investment. If the coefficient for Capital is positive, it suggests that an increase in the firm’s capital is associated with an increase in investment.
Magnitude: The magnitude of the coefficients indicates the strength of the relationship between the independent variables and the dependent variable. Larger coefficient values indicate a stronger impact on the dependent variable.
Statistical Significance: Check the p-values associated with the coefficients. A low p-value (typically below 0.05) indicates that the coefficient is statistically significant. Statistical significance suggests that the relationship between the independent variable and the dependent variable is unlikely due to random chance.
Interpretation : Interpretation of Coefficients: Intercept: The intercept term is -42.714369. This suggests that when both the Value and Capital are zero, the estimated investment is approximately -42.71. The intercept being statistically significant (p-value < 0.001) indicates that the model significantly deviates from a situation where all coefficients are zero.
Value Coefficient: The coefficient for Value is 0.115562. This implies that for every one unit increase in the firm’s value, the investment increases by approximately 0.1156 units. The Value coefficient is highly statistically significant (p-value < 0.001), indicating a strong relationship between Value and Investment.
Capital Coefficient: The coefficient for Capital is 0.230678. This suggests that for every one unit increase in the firm’s capital, the investment increases by approximately 0.2307 units. The Capital coefficient is also highly statistically significant (p-value < 0.001), indicating a strong relationship between Capital and Investment.
Overall Model Fit: Residuals: The residuals (errors) have a mean close to zero, indicating that the model is doing a good job of capturing the variation in the data.
R-squared: The R-squared value of 0.8124 suggests that approximately 81.24% of the variance in the dependent variable (Investment) is explained by the independent variables (Value and Capital).
F-statistic: The F-statistic with a very low p-value (< 0.001) indicates that the overall model is statistically significant.
Conclusion: The estimated coefficients make sense in terms of direction and magnitude. Both Value and Capital have positive coefficients, suggesting that they have a positive impact on Investment.
The coefficients for Value and Capital are highly statistically significant, indicating that the relationship between these variables and Investment is not due to random chance.
The high R-squared value and the low p-value of the F-statistic suggest that the model fits the data well and the independent variables are jointly significant in explaining the variance in Investment.
Considering the high statistical significance of the coefficients and the strong model fit, the estimated coefficients appear to be meaningful in explaining the relationship between Value, Capital, and Investment in the Grunfeld dataset.
Could there be omitted variable bias that could potentially be reduced by throwing in fixed effects?
Omitted variable bias can occur in regression analysis when a relevant variable that is correlated with both the independent variable(s) and the dependent variable is left out of the model. This can lead to biased and inconsistent coefficient estimates.
In the context of the OLS regression on the Grunfeld dataset, where we are estimating the investment equation using variables such as value and capital, there could be potential omitted variable bias if there are other factors that are correlated with both the independent variables and the dependent variable inv but are not included in the model.
Adding fixed effects (entity-specific effects) in a panel data analysis can help mitigate omitted variable bias by controlling for unobserved heterogeneity that is constant over time but varies across entities (firms in this case). Fixed effects model accounts for entity-specific effects that are constant over time, capturing any unobserved characteristics that could potentially bias the coefficient estimates.
# Estimate a fixed effects model
fe_model <- plm(inv ~ value + capital, data = Grunfeld, model = "within")
# Print the summary of the fixed effects model
summary(fe_model)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = inv ~ value + capital, data = Grunfeld, model = "within")
##
## Balanced Panel: n = 10, T = 20, N = 200
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -184.00857 -17.64316 0.56337 19.19222 250.70974
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## value 0.110124 0.011857 9.2879 < 2.2e-16 ***
## capital 0.310065 0.017355 17.8666 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 2244400
## Residual Sum of Squares: 523480
## R-Squared: 0.76676
## Adj. R-Squared: 0.75311
## F-statistic: 309.014 on 2 and 188 DF, p-value: < 2.22e-16
By running the above code, you estimate a fixed effects model on the Grunfeld dataset. The fixed effects model controls for individual-specific effects, which can help reduce omitted variable bias that might arise from unobserved factors specific to each firm.After estimating the fixed effects model and examining the results, you can compare the coefficients and statistical significance of the variables with the initial OLS regression to determine if including fixed effects has helped address potential omitted variable bias and improve the model’s accuracy.
Omitted variable bias can occur in regression analysis when a relevant variable that is correlated with both the independent variable(s) and the dependent variable is left out of the model. This can lead to biased and inconsistent coefficient estimates.
In the context of the OLS regression on the Grunfeld dataset, where we are estimating the investment equation using variables such as value and capital, there could be potential omitted variable bias if there are other factors that are correlated with both the independent variables and the dependent variable inv but are not included in the model.
Adding fixed effects (entity-specific effects) in a panel data analysis can help mitigate omitted variable bias by controlling for unobserved heterogeneity that is constant over time but varies across entities (firms in this case). Fixed effects model accounts for entity-specific effects that are constant over time, capturing any unobserved characteristics that could potentially bias the coefficient estimates.
Do your coefficients change? Why or why not?
Tell us what the fixed effects controlling for (time-invariant characteristics of the entity, or time-varying characteristics affecting all entities, or both - based on your specification)? It is common to include both time and entity fixed effects in many applications in Economics.
Do you get the same coefficient if you specify the Fixed Effect in an alternative way? Show (or at least argue).
Within Transformation (Entity Demeaning) Approach: This approach subtracts the entity-specific means from each observation, effectively removing the entity-specific effects. Estimating Equation: The within transformation subtracts the entity-specific mean from each variable:
inv it − invi =β1(valueit − valuei)+β2(capitalit− capitali)+uit
# Within transformation fixed effects model
fe_model_within <- plm(inv ~ value + capital, data = Grunfeld, model = "within")
summary(fe_model_within)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = inv ~ value + capital, data = Grunfeld, model = "within")
##
## Balanced Panel: n = 10, T = 20, N = 200
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -184.00857 -17.64316 0.56337 19.19222 250.70974
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## value 0.110124 0.011857 9.2879 < 2.2e-16 ***
## capital 0.310065 0.017355 17.8666 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 2244400
## Residual Sum of Squares: 523480
## R-Squared: 0.76676
## Adj. R-Squared: 0.75311
## F-statistic: 309.014 on 2 and 188 DF, p-value: < 2.22e-16
First Differences Estimator Approach: This approach takes the first difference of the variables, effectively differencing out the individual-specific effects.
Estimating Equation: The first differences estimator computes the change in variables between consecutive time periods: Δinvit = β1Δvalueit + β2Δcapitalit + Δuit
# First differences fixed effects model
fe_model_fd <- plm(inv ~ value + capital, data = Grunfeld, model = "fd")
summary(fe_model_fd)
## Oneway (individual) effect First-Difference Model
##
## Call:
## plm(formula = inv ~ value + capital, data = Grunfeld, model = "fd")
##
## Balanced Panel: n = 10, T = 20, N = 200
## Observations used in estimation: 190
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -200.889558 -13.889063 0.016677 9.504223 195.634938
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## (Intercept) -1.8188902 3.5655931 -0.5101 0.6106
## value 0.0897625 0.0083636 10.7325 < 2.2e-16 ***
## capital 0.2917667 0.0537516 5.4281 1.752e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 584410
## Residual Sum of Squares: 345460
## R-Squared: 0.40888
## Adj. R-Squared: 0.40256
## F-statistic: 64.6736 on 2 and 187 DF, p-value: < 2.22e-16
Dummy Variable Approach (Entity-Specific Intercept): This approach adds dummy variables for each entity, capturing the entity-specific effects directly.
Estimating Equation: The dummy variable approach includes entity-specific intercepts:
invit= β0+ β1valueit + β2capitalit + αi + uit
# Dummy variable fixed effects model
fe_model_dummy <- plm(inv ~ value + capital, data = Grunfeld, model = "within", effect = "individual")
summary(fe_model_dummy)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = inv ~ value + capital, data = Grunfeld, effect = "individual",
## model = "within")
##
## Balanced Panel: n = 10, T = 20, N = 200
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -184.00857 -17.64316 0.56337 19.19222 250.70974
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## value 0.110124 0.011857 9.2879 < 2.2e-16 ***
## capital 0.310065 0.017355 17.8666 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 2244400
## Residual Sum of Squares: 523480
## R-Squared: 0.76676
## Adj. R-Squared: 0.75311
## F-statistic: 309.014 on 2 and 188 DF, p-value: < 2.22e-16
In the above equations:
invit represents the investment for firm i in time period t.
value𝑖𝑡 represents the market value of the firm for firm 𝑖 in time period t.
capital𝑖𝑡 represents the real capital stock for firm 𝑖 in time period 𝑡.
invi, value i, and capital i denote the time-averaged values of the variables for each entity 𝑖.
𝛼𝑖 represents the entity-specific fixed effect in the dummy variable approach.
The coefficients in fixed effects models can change compared to OLS regression for several reasons, primarily due to the inclusion of entity-specific effects that are constant over time. Here’s why coefficients may change in fixed effects models:
Control for Unobserved Heterogeneity: Fixed effects models account for unobserved entity-specific characteristics that may be correlated with the independent variables. By controlling for these fixed effects, the coefficients may change as the model now captures the variation in the dependent variable that is specific to each entity.
Within-Entity Variation: Fixed effects models focus on within-entity variation over time, whereas OLS considers variation across all entities. This can lead to differences in coefficient estimates, especially if there are entity-specific trends or factors affecting the dependent variable.
Reduction of Omitted Variable Bias: Fixed effects models help reduce omitted variable bias by including entity-specific effects. This can alter the coefficients as the model now captures some of the variation that was previously attributed to omitted variables.
Efficiency and Consistency: Fixed effects estimation is generally more efficient and consistent than OLS when there are omitted variables that are constant within entities. This can lead to more reliable coefficient estimates.
Effect of Time-Invariant Variables: Fixed effects estimation controls for time-invariant variables (variables that do not change over time within entities) by design. This can impact the coefficients, especially if these variables are correlated with the independent variables.
Therefore, it’s common for coefficients to change in fixed effects models compared to OLS regression, as fixed effects methods provide a more refined understanding of the relationship between variables by explicitly modeling entity-specific effects. By accounting for these entity-specific effects, fixed effects models can provide more accurate and reliable coefficient estimates, particularly in panel data settings where entity-specific characteristics play a significant role in determining the dependent variable.
# Adding firm as a factor
Grunfeld$firm <- as.factor(Grunfeld$firm)
# using dummy variables in Fixed effects model
fe_model_dummy <- lm(inv ~ value + capital + firm, data = Grunfeld)
summary(fe_model_dummy)
##
## Call:
## lm(formula = inv ~ value + capital + firm, data = Grunfeld)
##
## Residuals:
## Min 1Q Median 3Q Max
## -184.009 -17.643 0.563 19.192 250.710
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -70.29672 49.70796 -1.414 0.159
## value 0.11012 0.01186 9.288 < 2e-16 ***
## capital 0.31007 0.01735 17.867 < 2e-16 ***
## firm2 172.20253 31.16126 5.526 1.08e-07 ***
## firm3 -165.27512 31.77556 -5.201 5.14e-07 ***
## firm4 42.48742 43.90988 0.968 0.334
## firm5 -44.32010 50.49226 -0.878 0.381
## firm6 47.13542 46.81068 1.007 0.315
## firm7 3.74324 50.56493 0.074 0.941
## firm8 12.75106 44.05263 0.289 0.773
## firm9 -16.92555 48.45327 -0.349 0.727
## firm10 63.72887 50.33023 1.266 0.207
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 52.77 on 188 degrees of freedom
## Multiple R-squared: 0.9441, Adjusted R-squared: 0.9408
## F-statistic: 288.5 on 11 and 188 DF, p-value: < 2.2e-16
#using plm for Fixed effects model with both entity and time fixed effects
FE_Model_Twoways <- plm(inv ~ value + capital, data = Grunfeld, model =
"within", effect = "twoways")
summary(FE_Model_Twoways)
## Twoways effects Within Model
##
## Call:
## plm(formula = inv ~ value + capital, data = Grunfeld, effect = "twoways",
## model = "within")
##
## Balanced Panel: n = 10, T = 20, N = 200
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -162.6094 -19.4710 -1.2669 19.1277 211.8420
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## value 0.117716 0.013751 8.5604 6.653e-15 ***
## capital 0.357916 0.022719 15.7540 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 1615600
## Residual Sum of Squares: 452150
## R-Squared: 0.72015
## Adj. R-Squared: 0.67047
## F-statistic: 217.442 on 2 and 169 DF, p-value: < 2.22e-16
Comparing the Fixed Effects and the dummy variables we could see that the coefficients for value i.e 0.1101 for fixed effect and 0.11012 for dummy variables and for capital it is 0.3101 for Fixed effect and 0.31007 for dummy variables are approximately same.
Therefore this consistency indicates that both methods are obtaining the same fixed effects for time-invariant firm characteristics.
In the Two-Way Fixed Effects Model: The Value Coefficient is a bit higher at 0.1177 compared to the one-way fixed effects model.
The Capital Coefficient is higher at 0.3579 compared to the one-way fixed effects model.
Therefore we could see that the increase in coefficients suggests that controlling for time-varying effects gets an additional variation which was not obtained in the one-way fixed effects model.