首页 > 解决方案 > 无法在 R 中使用“lrm.fit”拟合模型

问题描述

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

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

显示以下错误消息:

Unable to fit model using “lrm.fit”

这么说,我不是 R 方面的专家,并试图尽可能地帮助我的朋友。但是当我没有想法时,我希望有人能给我一个想法,为什么 lrm 没有在这段代码上运行。

示例数据截图:

示例数据

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

完整代码和参考:

# (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)

标签: rregressionlogistic-regressionmodel-fittingrms

解决方案


推荐阅读