简体   繁体   English

在虚拟变量的帮助下使用“linearHypothesis”命令时发生错误

[英]Error occurred while using "linearHypothesis" command with help of dummy variables

I am using WDI library to run commands regression analysis for 5 countries.我正在使用 WDI 库对 5 个国家/地区运行命令回归分析。

I try to create dummy variables with the help of command named as "dummy_cols".我尝试在名为“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).然后,我尝试使用这些虚拟变量表来运行 f-test,方法是使用命令“linearHypothesis”来检查国家越南和巴基斯坦的修复效果(例如)。 However, when I run the command of "linearHypothesis", it shows an error但是,当我运行“linearHypothesis”命令时,它显示错误

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"))

You just have to change the names in the hypothesis from country_Vietnam to countryVietnam (same for Pakistan).您只需将假设中的名称从country_Vietnam更改为countryVietnam (巴基斯坦也是如此)。 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.问题是您似乎在回归 model 中使用country作为虚拟对象,并且没有在回归 model 中明确包括country_Vietnamcountry_Pakistan列。 Hence, you have to use the variable names for Vietnam and Pakistan stored in your fit object reg2nn to form the hypothesis.因此,您必须使用存储在适合的 object reg2nn中的越南和巴基斯坦的变量名称来形成假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM