# Book retailing case — Data

来源：https://www.kaggle.com/competitions/booknew/data

本地 Meta Kaggle 快照；不是本次实时页面。

# Dataset Description

<P>There are two tables. The target.csv file has one record for every
customer in the training and test sets. The id variable uniquely 
identifies a customer and logtarg is the dependent variable. It is 
set to NA for the test cases. There should be 19,259 records plus a 
header line giving the variale names.
<P>The orders.csv file has one record for every item ordered by a 
customer in the test or training set prior to the contact point. 
There are 775012 records plus a header line. The variables are as 
follows:
<ul>
<LI>id: customer id matching the one in target.csv
<LI>ordnum: uniquely identifies an order
<LI>t: time in days since the order (1 is would be yesterday and 365 
would be one year ago).
<LI>category: category identifier for the item:
1=fiction; 3=classics; 5=cartoons; 6=legends; 7=philosophy;
8=religion; 9=psychology; 10=linguistics; 12=art; 14=music;
17=art reprints;
19=history; 20=contemporary history; 21=economy; 22=politics; 23=science;
26=computer science; 27=traffic, railroads; 30=maps; 31=travel guides;
35=health; 36=cooking; 37=learning; 38=games and riddles; 39=sports;
40=hobby; 41=nature/animals/plants; 44=encyclopedia; 50=videos, DVDs;
99=non books
<LI>qty: Number of items ordered
<LI>price: price per item (a person who orders qty=2 copies of a book 
for 10 Euros would have a total amount of 2*10=20 Euros)
<LI>brandid: the company has three separate brands selling different 
kinds of books. This offer is from brand 1.
</UL>

<P> Here is example code to get your started. Upload testanswer.csv.
<pre>
library(tidyverse)
setwd("/Users/ecm/teach/data/booknew")
orders = read.csv("orders.csv")
target = read.csv("target.csv")

rfm = orders %>%
  group_by(id) %>%
  summarise(tof=max(t), r = min(t), fitem=n(), ford=n_distinct(ordnum), m=sum(price*qty))
head(rfm)
summary(rfm)

all = target %>%
  left_join(rfm, by="id")
train=!is.na(all$logtarg)
fit = lm(logtarg ~ r + fitem + m, data=all, subset=train)
yhat = predict(fit, all[!train,])
ans = data.frame(id=all$id[!train], logtarg=yhat)
write.csv(ans, "testanswer.csv", row.names=F)
</pre>