如何在R中针对不同educ值检验回归模型的dem弹性显著性?
educ Values in R Using F-Tests Got it, let's break this down. Your elasticity formula for dem is:
Elasticity = βlog.dem + βlog.dem_educ * educ
You want to test if this elasticity equals 0 (i.e., is statistically significant) for educ values from 1 to 10. Your existing code works for educ=1 because the hypothesis simplifies to log.dem + log.dem_educ = 0—but for other values, we just need to adjust the linear constraint in linearHypothesis.
Step 1: Understand the General Hypothesis
For any educ value k, the null hypothesis we want to test is:
βlog.dem + βlog.dem_educ * k = 0
We can translate this into a string that linearHypothesis understands by plugging k directly into the constraint.
Step 2: Implement the Tests (with Robust SEs)
First, make sure you have the required packages loaded (car for linearHypothesis, sandwich for robust standard errors):
library(car) library(sandwich)
Then, define your target educ values and run the tests in a loop (or with lapply for cleaner code):
Option 1: Loop through each educ value
# Define the range of educ values to test educ_values <- 1:10 # Initialize a list to store test results elasticity_test_results <- list() for (k in educ_values) { # Build the hypothesis string dynamically hypothesis <- paste0("log.dem + ", k, "*log.dem_educ = 0") # Run the F-test with HC1 robust standard errors (matches your original code) test_out <- linearHypothesis(reg7, hypothesis, vcov = vcovHC(reg7, "HC1")) # Store the result with a name corresponding to the educ value elasticity_test_results[[as.character(k)]] <- test_out }
Option 2: Use lapply for a more concise approach
educ_values <- 1:10 elasticity_test_results <- lapply(educ_values, function(k) { hypothesis <- paste0("log.dem + ", k, "*log.dem_educ = 0") linearHypothesis(reg7, hypothesis, vcov = vcovHC(reg7, "HC1")) }) # Name the list elements for clarity names(elasticity_test_results) <- educ_values
Step 3: Access and Analyze Results
To view the test for a specific educ value (e.g., educ=5), just call:
elasticity_test_results[["5"]]
The key value to check is the Pr(>F) row—if this is below your significance threshold (usually 0.05), you can reject the null hypothesis and conclude the elasticity is statistically significant at that educ level.
Bonus: Organize Results into a Data Frame
For easier comparison across all educ values, you can extract the key statistics into a tidy data frame:
results_summary <- do.call(rbind, lapply(educ_values, function(k) { hypothesis <- paste0("log.dem + ", k, "*log.dem_educ = 0") test_out <- linearHypothesis(reg7, hypothesis, vcov = vcovHC(reg7, "HC1")) data.frame( educ = k, F_statistic = round(test_out$F[2], 3), p_value = round(test_out$`Pr(>F)`[2], 4), statistically_significant = ifelse(test_out$`Pr(>F)`[2] < 0.05, "Yes", "No") ) })) print(results_summary)
This will give you a clean table showing which educ values have a significant elasticity for dem.
内容的提问来源于stack exchange,提问作者Hassan A

