# Generating a matrix for regression from long form data

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.

• Are you just asking for help with the code here? Commented May 12, 2015 at 23:04
• No, more interested in the conceptual reasoning behind it. Just included the code so that there are examples. Thanks Commented May 12, 2015 at 23:11

> d1 = dcast(ID+success~channel, data=aa[-3])
Using channel as value column: use value.var to override.
Aggregation function missing: defaulting to length
> d2 = dcast(ID+success~viewTime, data=aa[-4])
Using viewTime as value column: use value.var to override.
Aggregation function missing: defaulting to length
>
> d1
ID success email tv web
1  a       1     1  2   0
2  b       0     1  1   1
3  c       0     1  1   1
4  d       1     1  1   1
> d2
ID success day night
1  a       1   0     3
2  b       0   3     0
3  c       0   0     3
4  d       1   3     0
>
> merge(d1, d2)
ID success email tv web day night
1  a       1     1  2   0   0     3
2  b       0     1  1   1   3     0
3  c       0     1  1   1   0     3
4  d       1     1  1   1   3     0


This table may be what you want.

• Thanks for your comment. However, I'm more concerned with interpretation once the tables are fed to the logistic regression. The merged table above (cc in my example) would give the main effects for the levels of channel and day vs night. If we then took table dd in my example, that would be both main effects and then all interactions, correct? Taking just table bb in my example is, I think, incorrect... (and really what I'm interested in figuring out). Thanks again Commented May 13, 2015 at 20:07
• I think merged table will be best: glm(success~ email+ tv+ web+ day+ night, family=binomial). It will give importance of each factor. You could also enter interaction terms if you want to assess their significance. You could also look at other options like association plot or correspondence analysis for this data.
– rnso
Commented May 14, 2015 at 1:34
• Although anova is generally not done on binary outcome variables, you can check the results of: aov(success~ (channel * viewTime)+ Error(Subject/(channel * viewTime)), mydata )
– rnso
Commented May 14, 2015 at 1:44