无法在 R 中使用“lrm.fit”拟合模型

问题描述

我试图帮助 R 上的一位朋友进行分析。这样做,我们陷入了 lrm:逻辑回归模型

ologit <- lrm(Y ~ X,data = myData)

显示以下错误消息:

Unable to fit model using “lrm.fit”

这么说,我不是 R 方面的专家,并试图尽可能地帮助我的朋友。但是由于我的想法不多了,我希望有人能告诉我为什么 lrm 没有在此代码上运行。

示例数据的屏幕截图:

Example Data

感谢您提供的任何帮助! :)

完整代码和参考:

# (1) INSTALL AND LOAD PACKAGES------------------------------------------------
#install.packages ("rms")
#install.packages ("tidyr")
#install.packages ("readr")
#install.packages ("dplyr")
#install.packages ("ggplot2")
#install.packages ("plm")
#install.packages ("MASS")
#install.packages ("plm")
#install.packages ("stargazer")

install.packages("rms")
install.packages("tidyr")
install.packages("readr")
install.packages("dplyr")
install.packages("ggplot2")
install.packages("plm")
install.packages("MASS")
install.packages("plm")
install.packages("stargazer")

library(rms)
library(tidyr)
library(readr)
library(dplyr)
library(ggplot2)
library(plm)
library(MASS)
library(plm)
library(stargazer)


# (2) IMPORT DATA SET-----------------------------------------------------------

library(readxl)
PanelDatanoFormatCat <- read_excel("inputFolder/PanelDatanoFormatCat.xlsx",sheet = "panel data",na = "na")
myData <- PanelDatanoFormatCat
attach(myData)

View(myData)

# (3) DATA DESCRIPTION----------------------------------------------------------
# DEPENDENT VARIABLE = Y = Gini Coefficient / Gini Category
# there are five categories:
#     Category 1:  0-19
#     Category 2: 20-39
#     Category 3: 40-59
#     Category 4: 60-79
#     Category 5: 80-100

# DEPENDENT VARIABLE Z = Poverty Headcount Ratio
# there are five categories:
#     Category 1:  0-19
#     Category 2: 20-39
#     Category 3: 40-59
#     Category 4: 60-79
#     Category 5: 80-100

#INDEPENDET VARIABLE = X
#List of independent variable
#     NominalGDP
#     GDPGrowth_annual_in_percent
#     GDPperCapita
#     UnemploymentTotal
#     LaborForcetotal
#     Inflation_annual_in_percent
#     GDPDeflator
#     CurrentAccountBalance
#     FDI_NetInflows
#     FDI_Netoutflows
#     ImportsGoodsServices_percent_GDP
#     ExportsGoodsServices_percent_GDP
#     StocksTraded_percent_GDP
#     GovernmentExpenditure_Education_Total
#     GovernmentExpenditurePerStudent_primary
#     GovernmentExpenditurePerStudent_secondary
#     GovernmenrtExpenditurePerStudent_tertiary
#     UrbanPopulation
#     IncomeShare_held_by_lowest_20percent
#     IncomeShare_held_by_highest_20percent
#     NetoADreceived
#     NetofficialDevelopmentAssistance_OfficialAidReceived
#     NetoDAReceivedPerCapita

# (4) ASSIGN X,Y AND Z VARIABLES-----------------------------------------------
Y <- cbind(GiniCategory)
Z <- cbind(PovertyCategory)
X <- cbind(NominalGDP,GDPGrowth_annual__in_percent,GDPperCapita,UnemploymentTotal,LaborForcetotal,Inflation_annual_in_percent,GDPDeflator,CurrentAccountBalance,FDI_NetInflows,FDI_Netoutflows,ImportsGoodsServices_percent_GDP,ExportsGoodsServices_percent_GDP,StocksTraded_percent_GDP,GovernmentExpenditureEducationTotal,GovernmentExpenditurePerStudent_primary,GovernmentExpenditurePerStudent_secondary,GovernmentExpenditruePerStudent_tertiary,UrbanPopulation,IncomeShare_held_by_lowest_20percent,IncomeShare_held_by_highest_20percent,Net_ODA_received,NetofficialDevelopmentAssistance_OfficialAidReceived,NetoDAReceivedPerCapita)
Xvar <- c("NominalGDP","GDPGrowth_annual_percent","GDPperCapita","UnemploymentTotal","LaborForcetotal","Inflation_annual_in_percent","GDPDeflator","CurrentAccountBalance","FDI_NetInflows","FDI_Netoutflows","ImportsGoodsServices_percent_GDP","ExportsGoodsServices_percent_GDP","StocksTraded_percent_GDP","GovernmentExpenditureEducationTotal","GovernmentExpenditurePerStudent_primary","GovernmentExpenditurePerStudent_secondary","GovernmentExpenditurePerStudent_tertiary","UrbanPopulation","IncomeShare_held_by_lowest_20percent","IncomeShare_held_by_highest_20percent","Net_ODA_received","NetofficialDevelopmentAssistance_OfficialAidReceived","NetoDAReceivedPerCapita")

# (5) DESCRIPTIVE STATISTICS----------------------------------------------------
summary(Y)
summary(X)
summary(Z)

# (6) REGRESSION----------------------------------------------------------------

# Using GiniCategory as a dependent variable

Regression <- lm(GiniCategory ~ NominalGDP + GDPGrowth_annual__in_percent +
                GDPDeflator + CurrentAccountBalance + FDI_NetInflows +
                FDI_Netoutflows + ImportsGoodsServices_percent_GDP +
                ExportsGoodsServices_percent_GDP + StocksTraded_percent_GDP +
                GovernmentExpenditureEducationTotal +
                GovernmentExpenditurePerStudent_primary +
                GovernmentExpenditurePerStudent_secondary +
                GovernmentExpenditruePerStudent_tertiary +
                UrbanPopulation + IncomeShare_held_by_lowest_20percent +
                IncomeShare_held_by_highest_20percent + Net_ODA_received +
                NetofficialDevelopmentAssistance_OfficialAidReceived +
                NetoDAReceived,data = myData)
summary(Regression)

lm(formula = GiniCategory ~ NominalGDP + GDPGrowth_annual__in_percent +
     GDPDeflator + CurrentAccountBalance + FDI_NetInflows +
     FDI_Netoutflows + ImportsGoodsServices_percent_GDP +
     ExportsGoodsServices_percent_GDP + StocksTraded_percent_GDP +
     GovernmentExpenditureEducationTotal +
     GovernmentExpenditurePerStudent_primary +
     GovernmentExpenditurePerStudent_secondary +
     GovernmentExpenditruePerStudent_tertiary +
     UrbanPopulation + IncomeShare_held_by_lowest_20percent +
     IncomeShare_held_by_highest_20percent + Net_ODA_received +
     NetofficialDevelopmentAssistance_OfficialAidReceived +
     NetoDAReceived,data = myData)

# (6.1) REGRESSION--------------------------------------------------------------

# using poverty headcount ratio as a dependent variable

Regression1 <- lm(PovertyCategory ~ NominalGDP + GDPGrowth_annual__in_percent +
                   GDPDeflator + CurrentAccountBalance + FDI_NetInflows +
                   FDI_Netoutflows + ImportsGoodsServices_percent_GDP +
                   ExportsGoodsServices_percent_GDP + StocksTraded_percent_GDP +
                   GovernmentExpenditureEducationTotal +
                   GovernmentExpenditurePerStudent_primary +
                   GovernmentExpenditurePerStudent_secondary +
                   GovernmentExpenditruePerStudent_tertiary +
                   UrbanPopulation + IncomeShare_held_by_lowest_20percent +
                   IncomeShare_held_by_highest_20percent + Net_ODA_received +
                   NetofficialDevelopmentAssistance_OfficialAidReceived +
                   NetoDAReceived,data = myData)
summary(Regression1)

lm(formula = PovertyCategory ~ NominalGDP + GDPGrowth_annual__in_percent +
     GDPDeflator + CurrentAccountBalance + FDI_NetInflows +
     FDI_Netoutflows + ImportsGoodsServices_percent_GDP +
     ExportsGoodsServices_percent_GDP + StocksTraded_percent_GDP +
     GovernmentExpenditureEducationTotal +
     GovernmentExpenditurePerStudent_primary +
     GovernmentExpenditurePerStudent_secondary +
     GovernmentExpenditruePerStudent_tertiary +
     UrbanPopulation + IncomeShare_held_by_lowest_20percent +
     IncomeShare_held_by_highest_20percent + Net_ODA_received +
     NetofficialDevelopmentAssistance_OfficialAidReceived +
     NetoDAReceived,data = myData)


# (7) ORDERED LOGIT MODEL COEFFICIENTS------------------------------------------
ddist <- datadist(Xvar)
options(datadist = 'ddist')

ologit <- lrm(Y ~ X,data = myData)
      

print(ologit)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)