r - 无法在 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)
解决方案
推荐阅读
- html - 如何在不拉伸的情况下将图像放入css-grid
- c# - 如何使用 C# 通过 Json 中的键名获取值列表
- google-apps-script - DriveApp 在环聊聊天机器人应用脚本中访问哪个帐户
- jenkins - 如何在声明性管道中打印插件变量?
- python - Python 的闭包是如何工作的
- java - 如何在 Ubuntu sublime-text3 中编译和运行 java 包中的文件?
- django - 使用 Django 中的日志记录捕获服务器错误 500(服务器 gunicorn)
- strapi - 如何从strapi.js中的关系中获取数据?
- python - 带有给定变量的 Python Dict 附加列表
- python - 拆分和使用 Excel 列中的值