Home > Mobile >  Error occurred while using "linearHypothesis" command with help of dummy variables
Error occurred while using "linearHypothesis" command with help of dummy variables

Time:12-23

I am using WDI library to run commands regression analysis for 5 countries.

I try to create dummy variables with the help of command named as "dummy_cols". I then try to use the those dummy variable table for running f-test by using command "linearHypothesis" to check the fix effect of country Vietnam and Pakistan (as example). However, when I run the command of "linearHypothesis", it shows an error

Error in constants(lhs, cnames_symb) : 
  The hypothesis "country_Vietnam=0" is not well formed: contains bad coefficient/variable names.
In addition: Warning message:
In constants(lhs, cnames_symb) : NAs introduced by coercion

following is the script I developed as student to run the analysis

Question2<- WDI(country= c("PH","VN", "LK","PK","IN"), indicator = c("NE.TRD.GNFS.ZS", "BX.KLT.DINV.WD.GD.ZS","NY.GDP.PCAP.PP.CD","SP.POP.TOTL","FP.CPI.TOTL.ZG"), start=1980, end=2020)

names(Question2)[names(Question2)=="NE.TRD.GNFS.ZS"]<- "Trade"
names(Question2)[names(Question2)=="BX.KLT.DINV.WD.GD.ZS"]<- "FDI"
names(Question2)[names(Question2)=="NY.GDP.PCAP.PP.CD"]<- "GDP"
names(Question2)[names(Question2)=="SP.POP.TOTL"]<- "Population"
names(Question2)[names(Question2)=="FP.CPI.TOTL.ZG"]<- "Inflation"


#splineinterpolation
install.packages("imputeTS")
library(imputeTS)
Question2lin<-na_interpolation(Question2, option = "spline")


#creating dummy variables

install.packages('fastDummies')
library('fastDummies')

reg2n<-dummy_cols(Question2lin,select_columns = 'country')
head(reg2n)

#regression equation with dummy variables
reg2nn<-lm(GDP~country Trade FDI Inflation Population, data = reg2n)
summary(reg2nn)

#fixed effect for country Vitenam and Pakistan
library(AER)
linearHypothesis(reg2nn, c("country_Vietnam=0","country_Pakistan=0"))

CodePudding user response:

You just have to change the names in the hypothesis from country_Vietnam to countryVietnam (same for Pakistan). So:

library(AER)
hypothesis <- c("countryVietnam = 0","countryPakistan = 0")
linearHypothesis(reg2nn, hypothesis)

The thing is that you seem to use country as a dummy in your regression model and not include the columns country_Vietnam and country_Pakistan explicitly in your regression model. Hence, you have to use the variable names for Vietnam and Pakistan stored in your fit object reg2nn to form the hypothesis.

  • Related