有没有办法使用价格弹性度量在 R 中绘制总收入曲线?
Is there a way to plot a Total Revenue Curve in R using a price elasticity measure?
我已经做了一整天了。
在 R 方面,我的水平低于平均水平,所以我还没有完全达到自己编写 scripts/functions 的水平(但尝试......失败了)
我有年销售额、收入、商品价格、收入中位数和人口的数据。
使用 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 轴上的不是收入,而是总销售额。
谢谢
展示了如何找到回归曲线的局部最大值。但是,您的回归模型没有局部最大值,因为模型在所有变量中都是一阶的。例如,该模型要求销售额随着价格的上涨总是增加或总是减少,这分别取决于价格的回归系数是正还是负。该模型需要至少是价格的二阶才能有一个(建模的)价格,在该价格下(建模的)销售额最大。
另一件需要注意的事情是,数据并未显示销售额和价格之间的简单关系。例如:
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
中是线性的,这意味着 Sales
与 Price
永远是一条线,而不是一条曲线。如果你想要一个允许 Sales
随着 Price
增加而下降然后上升(或上升然后下降)的模型,那么 Sales
需要至少是二次(二阶) ) Price
的函数(或比 Price
的线性函数具有更大灵活性的其他类型的模型)。
为了说明,让我们创建 Sales
与 Price
的线性和二次模型:
价格的线性(一阶)。下面的模型拟合这个等式:Sales = a + b * Price
,其中 a
和 b
是回归系数。
m5a = glm(Sales ~ Price, data=df)
价格的二次方(二阶)。 下面的模型拟合此等式:Sales = a + b * Price + c * Price^2
,其中 a
,b
,和 c
是回归系数。
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
现在让我们绘制这两个模型的预测以及数据。请注意,在下图中,线性价格模型只能朝一个方向发展。 Sales
随着 Price
的增加而以恒定速率增加。另一方面,二次价格模型是一个抛物线,其中 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))
我已经做了一整天了。 在 R 方面,我的水平低于平均水平,所以我还没有完全达到自己编写 scripts/functions 的水平(但尝试......失败了)
我有年销售额、收入、商品价格、收入中位数和人口的数据。
使用 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 轴上的不是收入,而是总销售额。
谢谢
另一件需要注意的事情是,数据并未显示销售额和价格之间的简单关系。例如:
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
中是线性的,这意味着 Sales
与 Price
永远是一条线,而不是一条曲线。如果你想要一个允许 Sales
随着 Price
增加而下降然后上升(或上升然后下降)的模型,那么 Sales
需要至少是二次(二阶) ) Price
的函数(或比 Price
的线性函数具有更大灵活性的其他类型的模型)。
为了说明,让我们创建 Sales
与 Price
的线性和二次模型:
价格的线性(一阶)。下面的模型拟合这个等式:Sales = a + b * Price
,其中 a
和 b
是回归系数。
m5a = glm(Sales ~ Price, data=df)
价格的二次方(二阶)。 下面的模型拟合此等式:Sales = a + b * Price + c * Price^2
,其中 a
,b
,和 c
是回归系数。
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
现在让我们绘制这两个模型的预测以及数据。请注意,在下图中,线性价格模型只能朝一个方向发展。 Sales
随着 Price
的增加而以恒定速率增加。另一方面,二次价格模型是一个抛物线,其中 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))