【问题标题】:Plotting Multiple Regression Parameters (ggplot in R Studio)绘制多个回归参数(R Studio 中的 ggplot)
【发布时间】:2021-06-19 15:23:02
【问题描述】:

** 更新:找到解决方案,请参阅底部的答案**

您好,我正在尝试复制一篇经济学论文。我有以下需要绘制的模型:

经济学论文数学模型/结果 ----------

变量:LNEARN、EDUC、EXP、ysm 和唯一的二进制变量 LANG(见下文) LNEARN ~ 0.049EDUC + 0.023EXP - 0.037 (EXP^2/100) + 0.028 ysm - 0.041(ysm^2/100) + LANG + 5.056

经济学论文中报告的变量均值-----------

如果修复我的一些变量很重要,这里是论文报告的变量的平均值,这些变量在我的 ggplot 中没有沿 x 和 y 变化:

平均报告的 EXP:22.76 平均报告 ysm:15.43

代码的图形输出附在下面的链接中

Current Output: Data-Generated Graph

# Current Code (Runs Properly) -----------------
# Creates model with data
# Recall only LANG takes on binary 0 and 1!
m_i <- lm(LNEARN ~ EDUC + EXP + I(EXP^2/100) + ysm + I(ysm^2/100) + as.factor(LANG), df_clean)

  ggplot(df_clean, aes(x = EDUC, y = LNEARN, color = LANG, size = ysm)) +
# Graphs data points
geom_point() +
# Data-generated regression
geom_smooth() +
# Cosmetic
xlab("Years of Formal Education") +
ylab("Log of Earnings") +
ggtitle("Education's Potential Impact On Immigrant Earnings") +
labs(subtitle = "1990 US Census Data", color = "Language", size = "Education") +
theme_pander()

【问题讨论】:

  • 我不确定你到底想做什么。如果您已经有了模型参数并且只想绘制它,为什么不使用这些参数创建一个函数并使用它来创建一个数据框?我不明白你为什么需要使用 lm 函数再次适应。
  • 我还没有正式学习如何做函数。我试图自学,但我所做的那些不断出现错误。你会怎么写出来?
  • 有一个geom_function。您为什么不使用它的 halp 页面和教程上的材料运行一些示例,您可能会假设除 EDUC 之外的所有变量的值都在他们的能力范围内。然后你应该能够在这些坐标上绘制估计的回归线。
  • ggeffects 包可能会有所帮助。
  • 如果您已经解决了您的问题,您能否将解决方案作为答案发布,而不是编辑您的问题以包含答案?

标签: r ggplot2 linear-regression replication economics


【解决方案1】:
# The problem was that my function in educ_equation_lang0 and educ_equation_lang1 didn't include * in the right places, so it was calling numbers I wanted to multiply as objects
# Useful troubleshooting was to check class(educ_equation_lang0) if it was read as a function
# Then I tried educ_equation_lang0(3) to see if the function could give an output (which it couldn't, and that's how I knew!)

m_i <- lm(LNEARN ~ EDUC + EXP + I(EXP^2/100) + ysm + I(ysm^2) + as.factor(LANG), df_clean)

educ_equation_lang0 <- function(x){0.049*x + 0.023*22.76 - 0.037*(22.76^2/100) + 0.028*15.43 - 0.041*(15.43^2/100) + 5.056}
educ_equation_lang1 <- function(x) {0.049*x + 0.023*22.76 - 0.037*(22.76^2/100) + 0.028*15.43 - 0.041*(15.43^2/100) + 1 + 5.056}

ggplot(df_clean, aes(x = EDUC, y = LNEARN, color = LANG, size = EXP)) +
    xlab("Years of Formal Education") +
    ylab("Log of Earnings") +
    ggtitle("Education's Potential Impact On Immigrant Earnings") +
    labs(subtitle = "1990 US Census Data", color = "Language \n (Speaks English 1 \n /Doesn't Speak 0)", size = "Education") +
    geom_point() +
    geom_function(fun = educ_equation_lang0, colour = "red") +
    geom_function(fun = educ_equation_lang1, colour = "lightblue") +
    geom_smooth() +
    theme_pander()

Graph to Final Solution

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 2013-07-11
    • 2017-02-21
    • 2020-05-08
    • 2019-05-19
    • 2018-02-24
    • 2017-05-06
    • 1970-01-01
    • 2017-01-21
    相关资源
    最近更新 更多