【问题标题】:Is there a way to plot a Total Revenue Curve in R using a price elasticity measure?有没有办法使用价格弹性度量在 R 中绘制总收入曲线?
【发布时间】:2019-05-20 10:25:09
【问题描述】:

我整天都在忙。 在 R 方面,我的水平相当低于平均水平,所以我还没有达到编写自己的脚本/函数的水平(但正在尝试......但失败了)

我有年销售额、收入、商品价格、收入中位数和人口的数据。

使用 glm,我消除了一些变量(自相关、无意义等)。

这是我正在使用的数据框的输入:

dput(df)
structure(list(Year = 2008:2018, Sales = c(50681L, 53016L, 53981L, 
56204L, 55564L, 56916L, 61328L, 59686L, 59412L, 57298L, 57569L
), Population = c(9250000L, 9380000L, 9570000L, 9660000L, 9760000L, 
9850000L, 9940000L, 10040000L, 10160000L, 10270000L, 10454860L
), Income = c(52941L, 53127L, 50020L, 48816L, 47969L, 48294L, 
48385L, 48253L, 49489L, 51672L, 51752L), Price_up = c(15, 15.57, 
15.50772, 15.75584352, 16.26003051, 16.60149115, 20, 20.32, 20.34032, 
20.60474416, 21.03744379), Price = c(16.60149115, 16.26003051, 
15.75584352, 15.50772, 15.57, 15, 21.03744379, 20.60474416, 20.34032, 
20.32, 20), ad_revenue = c(1293145, 1270159.59, 1297991.2, 1362019.86, 
1330311.32, 1423933.04, 1499699.64, 1983487.176, 2034322.84, 
2010148.6, 2008107.84)), class = "data.frame", row.names = c(NA, 
11L))

#Run Models#
m1 <- glm(formula = Sales ~ Price, data = df)
m2 <- update(m1, . ~ . + Income)
m3 <- update(m2, . ~ . + ad_revenue)
m4 <- update(m3, . ~ . + Population)
library(memisc)
library(car)

#m3 is best# 
mtable(m1, m2, m3, m4) 

#No autocorrelation# 
durbinWatsonTest(m3) 

#Calculate Price Elasticity# 
PEm3 <- as.numeric(m3$coefficients['Price'] * mean(df$Price)/mean(df$Sales))

那么,我如何确定最优价格?是否可以使用 ggplot 绘制曲线来指示在销售额下降之前可以收取最高价格的位置?

价格弹性值为1>x>-1,表示相对缺乏弹性。

请帮我展示一条钟形曲线,表示销售暴跌前的最高价格点。它看起来像这里的图片:

不同之处在于,y 轴上的收入不是总销售额。

谢谢

【问题讨论】:

  • 您的代码似乎需要加载memisccar 库。
  • 是的,我忘记添加了。

标签: r ggplot2 economics


【解决方案1】:

This StackOverflow answer 展示了如何找到回归曲线的局部最大值。但是,您的回归模型没有局部最大值,因为模型在所有变量中都是一阶的。例如,该模型要求销售额总是随着价格的增加而增加或减少,这分别取决于价格的回归系数是正的还是负的。该模型需要在价格上至少是二阶的,才能有一个(模型化的)价格,在该价格上(模型化的)销售额达到最大值。

另外需要注意的是,数据并未显示销售额和价格之间的简单关系。例如:

library(ggplot2)
theme_set(theme_classic())

ggplot(df, aes(Price, Sales)) + 
  geom_line(colour="grey80", size=1) +
  geom_point()

如果我们按年份顺序绘制点,看起来宏观经济因素混淆了销售与价格的关系:

ggplot(df, aes(Price, Sales)) + 
  geom_path(colour="grey80", size=1) +
  geom_text(aes(label=Year), colour='red') + 
  theme_classic()

更新:回答您评论中的问题:您示例中的模型在Price 中是线性的,这意味着SalesPrice 将始终是一条线,而不是一条曲线。如果您想要一个模型,其中允许Sales 随着Price 的增加而下降然后上升(或上升然后下降),那么Sales 需要至少是Price 的二次(二阶)函数(或比Price 的线性函数更灵活的其他类型的模型)。

为了说明,让我们创建SalesPrice 的线性和二次模型:

价格中的线性(一阶)。下面的模型拟合这个方程:Sales = a + b * Price,其中ab 是回归系数。

m5a = glm(Sales ~ Price, data=df)

价格的二次(二阶)。下面的模型拟合这个方程:Sales = a + b * Price + c * Price^2,其中abc 是回归系数。 p>

m5b = glm(Sales ~ Price + I(Price^2), data=df)
# m5b = glm(Sales ~ poly(Price, 2, raw=TRUE), data=df)  # Another way to specify the quadratic model

现在让我们将这两个模型的预测与数据一起绘制出来。请注意,在下图中,线性价格模型只能朝一个方向发展。随着Price 的增加,Sales 以恒定的速率增加。另一方面,价格二次模型是一条抛物线,其中Sales 最初随着Price 的增加而下降,然后随着Price 的增加而上升。

二次模型更好地拟合数据,但两种模型在经济上似乎都没有多大意义。

# Set up data frame for predictions
pred.dat = data.frame(Price = seq(min(df$Price), max(df$Price), length=100))

# Add predictions from the two models
pred.dat$linear = predict(m5a, newdata=pred.dat)
pred.dat$quadratic = predict(m5b, newdata=pred.dat)

# Reshape prediction data to long format and plot
pred.dat %>% gather(Model, Sales, -Price) %>% 
  ggplot(aes(Price, Sales)) +
    geom_point(data=df) +  # Add data points
    geom_line(aes(colour=Model))

【讨论】:

  • 我觉得如果多年来有更多的数据点,混杂因素会减少一点。您所说的“模型需要在价格上至少是二阶的,才能有一个(模型化的)销售量达到最大值的(模型化的)价格。”是什么意思?
  • 这很有用,我现在完全明白你的意思了。
猜你喜欢
  • 2022-07-07
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2021-09-19
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
相关资源
最近更新 更多