有没有一种方法可以使用价格弹性度量在R中绘制总收入曲线?

有没有一种方法可以使用价格弹性度量在R中绘制总收入曲线?,r,ggplot2,economics,R,Ggplot2,Economics,我已经做了一整天了。 说到R,我的平均水平很低,所以我还没有完全达到编写自己的脚本/函数的水平(但是尝试了..却失败了) 我有年销售额、收入、商品价格、收入中值和人口数据 使用glm,我消除了一些变量(自相关、无意义等) 下面是我正在使用的数据帧的dput: dput(df) structure(list(Year = 2008:2018, Sales = c(50681L, 53016L, 53981L, 56204L, 55564L, 56916L, 61328L, 59686L, 594

我已经做了一整天了。 说到R,我的平均水平很低,所以我还没有完全达到编写自己的脚本/函数的水平(但是尝试了..却失败了)

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

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

下面是我正在使用的数据帧的dput:

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))
dput(df)
结构(列表)(年份=2008:2018,销售额=c)(50681L,53016L,53981L,
56204L、55564L、56916L、61328L、59686L、59412L、57298L、57569L
),人口=c(9250000L,9380000L,9570000L,9660000L,9760000L,
9850000L、9940000L、10040000L、10160000L、10270000L、10454860L
),收入=c(52941L,53127L,50020L,48816L,47969L,48294L,
48385L、48253L、49489L、51672L、51752L),价格上涨=c(15,15.57,
15.50772, 15.75584352, 16.26003051, 16.60149115, 20, 20.32, 20.34032, 
20.60474416,21.03744379),价格=c(16.60149115,16.26003051,
15.75584352, 15.50772, 15.57, 15, 21.03744379, 20.60474416, 20.34032, 
广告收入=c(12931451270159.591297991.21362019.86,
1330311.32, 1423933.04, 1499699.64, 1983487.176, 2034322.84, 
2010148.62008107.84),class=“data.frame”,row.names=c(NA,
(11升)
#运行模型#
m1显示了如何找到回归曲线的局部最大值。但是,回归模型没有局部最大值,因为模型在所有变量中都是一阶的。例如,该模型要求销售额总是随着价格的上涨而增加,或者总是随着价格的上涨而减少,这分别取决于价格回归系数是正还是负。模型需要至少是二阶价格,才能有一个(模型化)价格,在该价格下(模型化)销售额为最大值

另一件需要注意的事情是,数据并没有显示销售和价格之间的简单关系。例如:

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

更新:回答您评论中的问题:您示例中的模型在
价格
中是线性的,这意味着
销售
价格
将始终是一条直线,而不是一条曲线。如果您想要一个模型,在该模型中,
销售
可以随着
价格
的上涨而先下降后上升(或先上升后下降),那么
销售
至少需要是
价格
的二次(二阶)函数(或比
价格
的线性函数更灵活的其他类型的模型)

为了举例说明,让我们创建
销售
价格
的线性和二次模型:

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

m5a = glm(Sales ~ Price, data=df)
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
二次(二阶)价格。下面的模型正拟合这个方程:
销售=a+b*价格+c*价格^2
,其中
a
b
,和
c
是回归系数

m5a = glm(Sales ~ Price, data=df)
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
现在,让我们将这两个模型的预测与数据一起绘制出来。请注意,下图中的线性价格模型只能朝一个方向发展<代码>销售额
随着
价格
的上涨而以固定的速度增长。另一方面,二次价格模型是一条抛物线,
销售
最初随着
价格
的增加而下降,然后随着
价格
的增加而上升

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

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

看起来您的代码需要加载
memisc
car
库。是的,我忘了在库中添加它。我觉得如果多年来有更多的数据点,混淆因素会最小化一点。你所说的“模型需要至少是二阶价格,才能有一个(模型化的)价格,在这个价格下(模型化的)销售额是最大的”是什么意思?这是非常有用的,我现在完全理解你的意思了。