I'm having some difficulty interpreting how to correctly create a matrix input for regression from a long form data source. I have table containing marketing data where each row represents a view of an advert, with person ID, time of day and channel also included, and purchase decision for that person (success).
This R code creates a rough sample of the long form data:
aa <- data.frame(ID=rep(letters[1:4]), success=c(1,0,0,1,1,0,0,1,1,0,0,1),
+ viewTime=rep(c("night","day")), channel=rep(c("tv","web","email"),
+ c(5,3,4)))
aa
ID success viewTime channel
1 a 1 night tv
2 b 0 day tv
3 c 0 night tv
4 d 1 day tv
5 a 1 night tv
6 b 0 day web
7 c 0 night web
8 d 1 day web
9 a 1 night email
10 b 0 day email
11 c 0 night email
12 d 1 day email
To model this I'm interested in summarizing the data at the person ID level and then fitting a logistic regression based on the purchase decision (success). I'm curious whether time of day, channel, and the interactions between time and channel influence the decision.
The problem I'm having is how to summarize this. I can summarize across both variables of interest like this:
library(reshape2)
> bb <- dcast(aa, ID + success ~ channel + viewTime)
> bb
ID success email_day email_night tv_day tv_night web_day web_night
1 a 1 0 1 0 2 0 0
2 b 0 1 0 1 0 1 0
3 c 0 0 1 0 1 0 1
4 d 1 1 0 1 0 1 0
Which returns the count of each combination of channel/time across each person, but I worry that this would model the interaction without modeling the main effect, which I know to be incorrect. Another option would be to summarize the count of ads by each variable of interest independently, like this:
library(dplyr)
cc <- left_join(dcast(aa, ID + success ~ viewTime), dcast(aa, ID + success ~ channel))
> cc
ID success day night email tv web
1 a 1 0 3 1 2 0
2 b 0 3 0 1 1 1
3 c 0 0 3 1 1 1
4 d 1 3 0 1 1 1
But now it seems rather odd to assign separate count data to each view, e.g. the count of views per person is double-counted. A third option is to join the two previous tables together.
dd <- left_join(cc,bb)
> dd
ID success day night email tv web email_day email_night tv_day tv_night web_day web_night
1 a 1 0 3 1 2 0 0 1 0 2 0 0
2 b 0 3 0 1 1 1 1 0 1 0 1 0
3 c 0 0 3 1 1 1 0 1 0 1 0 1
4 d 1 3 0 1 1 1 1 0 1 0 1 0
Which returns individual counts for Time
and Channel
, as well as the count across each possible interaction. My question is which of these three approaches would be most correct and what is the reasoning behind that.