我做了一个函数sales.deriv。它的定义位于页面底部。该函数从给定的数据框中查找销售数据的衍生物并绘制它。 (他们对我的编码方式可能效率很低。请随意评论它们,但清理它们不是我的重点。)
Command One,
> sales.deriv(trans.df=subset(trans, Week < 27 & Week > 8 & Day == "Wednesday"))
返回Plot One:
命令二,
> Day <- "Wednesday"
> sales.deriv(trans.df=subset(trans, Week < 27 & Week > 8 & Day == Day))
返回Plot 2:
正如您所看到的,图表是不同的 - 最明显的是11之前的相对峰值数量。但是,通过定义Day <- "Wednesday"
,在我看来命令应该是相同的。这是怎么回事?
功能定义:
sales.deriv <- function(trans.df, plot=TRUE, xmin=7, xmax=18, ymin=0, ymax=150, title = "Rate of Revenue", pch=1, col="black", n=50, cex=1)
{
trans$Net.Sales[is.na(trans$Net.Sales)] <- 0
trans.time <- trans.df[order(trans.df$Time.Dec),]
fake.bins <- ddply(trans.df[c("Date","Net.Sales")], .(Date), summarize, Net.Sales = sum(Net.Sales))
trans.time$Cum.Sum <- cumsum(trans.time$Net.Sales) / nrow(fake.bins)
time.fit2 <- lm(Cum.Sum ~ poly(Time.Dec, n, raw=T), data=subset(trans.time, Hour >= 6 | Time.Dec<20))
sales.rate <- data.frame(Time.Dec=seq(from=xmin, to=xmax, by=.0333))
sales.rate$Cum.Sum <- predict(time.fit2, sales.rate)
dYdX <- diff(sales.rate$Cum.Sum)/diff(sales.rate$Time.Dec)
sales.rate$dYdX <- c(0,dYdX)
if (!plot) {
points(dYdX~Time.Dec, data=sales.rate, col=col, xlim=c(xmin,xmax), ylim=c(ymin,ymax), main=title, cex=cex, pch=pch)
} else {
plot(dYdX~Time.Dec, data=sales.rate, col=col, xlim=c(xmin,xmax), ylim=c(ymin,ymax), main=title, pch=pch, cex=cex, xlab="Time", ylab="Revenue per Hour", xaxt="n")
axis(1, at=seq(from=xmin, to=xmax, by=1))}}