使用nls()和预测变量进行R低音扩散

时间:2018-11-10 02:47:26

标签: r nls

研究人员在估计低音曲线时如何包括其他预测变量/独立变量?我读过,Bass扩散不接受其他预测变量,但是我看到有论文声称它们可以。这么说,用R中的nls()函数可以做到吗?我创建了一些玩具数据和一个没有额外预测变量 advert 的示例。

TIA

library(data.table)

options(scipen=999)
rm(list=ls())

df <- data.table(
  year  = seq(1979, 1988, by=1), 
  T     = 1:10, 
  sales = c(840, 1470, 2110, 4000, 7590, 10950, 10530, 9470, 7790, 5890),
  advert = c(100,100,100,100,100,75,75,50,50,25))
df[, sales_cumulative := cumsum(sales)]

est_bass <- nls(sales ~ M*(((P+Q)^2/P)*exp(-(P+Q)*T))/(1+(Q/P)*exp(-(P+Q)*T))^2,
                # add P and Q below
                start=c(list(M = 60630, P = 0.03, Q = 0.38)),
                data = df)
coef(est_bass)

# P, Q, M parameters
m  = coef(est_bass)[1]
p  = coef(est_bass)[2]
q  = coef(est_bass)[3]

Tdelt <- (1:100)/10
ngete <- exp(-(p+q)*Tdelt)

# plot pdf
par(mfrow=c(1,2))

Bpdf <- m*((p+q)^2/p)*ngete/(1+(q/p)*ngete)^2
plot(Tdelt, Bpdf, xlab = "Year from 1979", ylab = "Sales per year", type = "l")
points(df$T, df$sales) # compare to original

# plot cdf
Bcdf <- m*(1-ngete)/(1+(q/p)*ngete)
plot(Tdelt, Bcdf, xlab = "Year from 1979", ylab = "Cumulative sales", type = "l")
points(df$T, df$sales_cumulative)

0 个答案:

没有答案