我正在使用 WDI 庫為 5 個國家/地區運行命令回歸分析。
我嘗試在名為“dummy_cols”的命令的幫助下創建虛擬變數。然后我嘗試使用這些虛擬變數表運行 f-test,通過使用命令“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"))
uj5u.com熱心網友回復:
您只需將假設中的名稱從 更改country_Vietnam為countryVietnam(巴基斯坦相同)。所以:
library(AER)
hypothesis <- c("countryVietnam = 0","countryPakistan = 0")
linearHypothesis(reg2nn, hypothesis)
問題是您似乎country在回歸模型中用作虛擬物件,而不是在回歸模型中明確包含列country_Vietnam和列country_Pakistan。因此,您必須使用存盤在擬合物件中的越南和巴基斯坦的變數名稱reg2nn來形成假設。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/390442.html
上一篇:R函式按觀察年份劃分人-時間
