在虚拟变量的帮助下使用 "linearHypothesis" 命令时出错
Error occurred while using "linearHypothesis" command with help of dummy variables
我正在使用 WDI 库 运行 命令对 5 个国家进行回归分析。
我尝试借助名为“dummy_cols”的命令创建虚拟变量。然后,我尝试使用这些虚拟变量 table 进行 运行ning f 测试,方法是使用命令“linearHypothesis”来检查国家越南和巴基斯坦的修复效果(例如)。但是,当我运行命令“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
以下是我作为学生开发的脚本,用于运行分析
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"))
您只需将假设中的名称从 country_Vietnam
更改为 countryVietnam
(巴基斯坦也一样)。所以:
library(AER)
hypothesis <- c("countryVietnam = 0","countryPakistan = 0")
linearHypothesis(reg2nn, hypothesis)
问题是您似乎在回归模型中使用 country
作为虚拟变量,并且未在回归模型中明确包含列 country_Vietnam
和 country_Pakistan
。因此,您必须使用存储在拟合对象中的越南和巴基斯坦的变量名称 reg2nn
来形成假设。
我正在使用 WDI 库 运行 命令对 5 个国家进行回归分析。
我尝试借助名为“dummy_cols”的命令创建虚拟变量。然后,我尝试使用这些虚拟变量 table 进行 运行ning f 测试,方法是使用命令“linearHypothesis”来检查国家越南和巴基斯坦的修复效果(例如)。但是,当我运行命令“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
以下是我作为学生开发的脚本,用于运行分析
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"))
您只需将假设中的名称从 country_Vietnam
更改为 countryVietnam
(巴基斯坦也一样)。所以:
library(AER)
hypothesis <- c("countryVietnam = 0","countryPakistan = 0")
linearHypothesis(reg2nn, hypothesis)
问题是您似乎在回归模型中使用 country
作为虚拟变量,并且未在回归模型中明确包含列 country_Vietnam
和 country_Pakistan
。因此,您必须使用存储在拟合对象中的越南和巴基斯坦的变量名称 reg2nn
来形成假设。