R. Same command gives different plots. Variable vs. Character string -
i lied. aren't quite same commands. don't see how different.
i made function sales.deriv. it's definition @ bottom of page. function finds derivative of sales data given dataframe , plots it. (they're plenty of inefficiencies in how coded it. feel free comment on them cleaning them not focus here.)
command one,
> sales.deriv(trans.df=subset(trans, week < 27 & week > 8 & day == "wednesday"))
returns plot one:
command two,
> day <- "wednesday" > sales.deriv(trans.df=subset(trans, week < 27 & week > 8 & day == day))
returns plot two:
as can see, plots different — noticeably in number of relative peaks before 11. defining day <- "wednesday"
, though, seems me commands should identical. what's going on here?
function definition:
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))}}
Comments
Post a Comment