Docsity

regression analysis homework, Summaries of Statistics

Toggle sidebar
bg1
T
hi
s
not
eb
oo
k
s
h
oul
d
be
s
e
t
to
t
he
c
orr
ec
t
K
e
rn
e
l
bef
or
e
wor
ki
n
g
on
t
he
qu
e
st
i
ons
.
S
a
l
e
s
a
n
d
C
ustom
e
r
I
nt
e
r
ac
t
i
on
D
a
t
a
s
e
t
I
n
h
om
e
wor
k
2,
w
e
agai
n
v
i
s
i
t
t
he
s
a
l
e
s
da
t
a
s
e
t
us
ed
i
n
h
om
e
wor
k
1.
A
s
a
r
e
m
i
n
de
r
,
t
he
da
t
a
s
e
t
c
ont
ai
ns
t
he
f
ollow
i
n
g
c
olumns
:
1)
C
ustom
e
r
_
ID
:
U
n
i
qu
e
ide
nt
ifie
r
f
or
each
c
ustom
e
r
.
2)
P
ro
d
u
c
t
_
C
a
t
eg
ory
:
C
a
t
eg
ory
o
f
pur
cha
s
ed
pro
d
u
c
t
(
e
.
g
.
'
B
oo
k
s
',
'
E
l
ec
tron
ic
s
',
'
T
oys
',
'
C
lot
hi
n
g
',
e
t
c
.).
3)
P
ur
cha
s
e
_
A
mount
:
T
ot
a
l
a
mount
,
i
n
d
oll
a
rs
,
sp
e
nt
on
each
pur
cha
s
e
.
4)
P
ur
cha
s
e
_
D
a
t
e
:
D
a
t
e
o
f
tr
a
ns
ac
t
i
on
.
5)
C
ustom
e
r
_
A
ge
:
A
ge
o
f
c
ustom
e
r
a
t
t
i
m
e
o
f
pur
cha
s
e
.
6)
C
ustom
e
r
_
G
e
n
de
r
:
G
e
n
de
r
o
f
t
he
c
ustom
e
r
('
M
a
l
e
'
or
'
F
e
m
a
l
e
').
7)
S
tor
e
_
L
o
ca
t
i
on
:
L
o
ca
t
i
on
o
f
stor
e
w
he
r
e
pur
cha
s
e
o
cc
urr
ed
.
8)
S
a
t
i
s
fac
t
i
on
_
S
c
or
e
(
r
e
spons
e
v
a
r
iab
l
e
)
:
N
um
e
r
ic
r
e
spons
e
v
a
r
iab
l
e
i
n
dica
t
i
n
g
c
ustom
e
r
'
s
s
a
t
i
s
fac
t
i
on
l
e
v
e
l
f
rom
0
(
l
ea
st
s
a
t
i
s
fied
)
to
10
(
most
s
a
t
i
s
fied
).
set.seed(17) # Seed set to ensure reproducible results. DO NOT CHANGE SEED O
# Load the dataset
sales_data <- read.csv("cleaned_sales_dataset.csv", header = TRUE)
# Convert specified columns to categorical variables
sales_data$Product_Category <- as.factor(sales_data$Product_Category)
sales_data$Customer_Gender <- as.factor(sales_data$Customer_Gender)
sales_data$Store_Location <- as.factor(sales_data$Store_Location)
# Convert Purchase_Date to Date format
sales_data$Purchase_Date <- as.Date(sales_data$Purchase_Date, format="%m/%d/
# Split the data into 80% training and 20% test sets
sample_int_ceiling <- sample.int(n = nrow(sales_data),
size = ceiling(.80 * nrow(sales_data)), rep
train_sales_data <- sales_data[sample_int_ceiling, ]
test_sales_data <- sales_data[-sample_int_ceiling, ]
# Remove row names
rownames(train_sales_data) <- NULL
rownames(test_sales_data) <- NULL
In[2]:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download regression analysis homework and more Summaries Statistics in PDF only on Docsity!

This notebook should be set to the correct Kernel

before working on the questions.

Sales and Customer Interaction Dataset

In homework 2, we again visit the sales dataset used in homework 1. As a reminder, the dataset contains the following columns:

1) C ustom e r _ID : Unique identifier for each customer.

2) P ro d u c t _Ca t eg ory: Category of purchased product (e.g. 'Books', 'Electronics', 'Toys', 'Clothing', etc.).

3) P ur cha s e_A mount: Total amount, in dollars, spent on each purchase.

4) P ur cha s e_Da t e : Date of transaction.

5) C ustom e r _Age : Age of customer at time of purchase.

6) C ustom e r _Ge n de r: Gender of the customer ('Male' or 'Female').

7) S tor e_L o ca t i on: Location of store where purchase occurred.

8) Sa t i s fac t i on _Sc or e ( r e spons e v a r iab l e) : Numeric response variable indicating customer's satisfaction level from 0 (least satisfied) to 10 (most satisfied).

set.seed( 17 ) # Seed set to ensure reproducible results. DO NOT CHANGE SEED O

# Load the dataset sales_data <- read.csv("cleaned_sales_dataset.csv", header = TRUE )

# Convert specified columns to categorical variables sales_data $ Product_Category <- as.factor(sales_data $ Product_Category) sales_data $ Customer_Gender <- as.factor(sales_data $ Customer_Gender) sales_data $ Store_Location <- as.factor(sales_data $ Store_Location)

# Convert Purchase_Date to Date format sales_data $ Purchase_Date <- as.Date(sales_data $ Purchase_Date, format = "%m/%d/

# Split the data into 80% training and 20% test sets sample_int_ceiling <- sample.int(n = nrow(sales_data), size = ceiling(. 80 ***** nrow(sales_data)), rep train_sales_data <- sales_data[sample_int_ceiling, ] test_sales_data <- sales_data[ - sample_int_ceiling, ]

# Remove row names rownames(train_sales_data) <- NULL rownames(test_sales_data) <- NULL

In [2]:

Question 1: Data Preparation (6 points)

1 a) U s i n g t he da t af r a m e, " s a l e s _da t a", sort b y t he Sa t i s fac t i on _Sc or e (f rom low to high), pr i nt t he fi rst 10 rows o f t he da t af r a m e, t he n pr i nt m i n i mum , m a x i mum , 10 t h p e r ce nt i l e, 25 t h p e r ce nt i l e, 50 t h p e r ce nt i l e, 75 t h p e r ce nt i l e, 90 t h p e r ce nt i l e, a v e r age, a n d m edia n Sa t i s fac t i on _Sc or e. N ot e: t he sort i n g s h oul d be p e rm a n e ntly ( not t e mpor a r i ly ) stor ed unl e ss ot he rw i s e st a t ed. (3 points)

sales_data <- sales_data[order(sales_data $ Satisfaction_Score),] head(sales_data, 10 )

sprintf("Min satisfaction: %f", min(sales_data $ Satisfaction_Score)) sprintf("Max satisfaction: %f", max(sales_data $ Satisfaction_Score)) sprintf("Average satisfaction: %f", mean(sales_data $ Satisfaction_Score)) sprintf("Median satisfaction: %f", median(sales_data $ Satisfaction_Score)) sprintf("10th percentile: %f", quantile(sales_data $ Satisfaction_Score,. 10 )) sprintf("25th percentile: %f", quantile(sales_data $ Satisfaction_Score,. 25 )) sprintf("50th percentile: %f", quantile(sales_data $ Satisfaction_Score,. 50 )) sprintf("75th percentile: %f", quantile(sales_data $ Satisfaction_Score,. 75 )) sprintf("90th percentile: %f", quantile(sales_data $ Satisfaction_Score,. 90 ))

A data.frame: 10 × 8 Customer_ID Product_Category Purchase_Amount Purchase_Date Customer_Age

743 850 Toys 143.16 2024-10-21 30

593 713 Toys 39.94 2024-09-03 20

440 555 Toys 374.99 2024-09-25 52

777 883 Toys 463.96 2024-08-19 26

269 359 Clothing 453.28 2024-03-17 37

36 136 Toys 40.29 2024-06-07 62

426 538 Toys 384.70 2024-12-04 42

245 335 Toys 555.06 2024-07-01 54

868 969 Toys 63.00 2024-04-02 35

872 972 Toys 655.22 2024-06-18 22

'Min satisfaction: 2.727493' 'Max satisfaction: 10.000000' 'Average satisfaction: 7.475521' 'Median satisfaction: 7.508549' '10th percentile: 5.665529' '25th percentile: 6.411953' '50th percentile: 7.508549'

In [32]:

A grouped_df: 13 × 2 Customer_ID Store_Location

199 Chicago

1058 Los Angeles

1058 Denver

199 New York

199 Miami

1058 New York

199 Seattle

1058 Seattle

199 Denver

1058 Seattle

199 Dallas

1058 San Francisco

199 Denver

Response: No, none of the frequent customers have made more than 5 purchases at a single store location.

Question 2: Data preparation (9 points)

2 a) C r ea t e a w e ll - l abe l ed st acked ba r cha rt s h ow i n g t he proport i on o f pur cha s e s f rom each P ro d u c t _Ca t eg ory i n each S tor e_L o ca t i on. N ot e: S tor e_L o ca t i on s h oul d be on t he x - a x i s , proport i on o f pur cha s e s s h oul d be on t he y - a x i s , a n d t he st acked ba r cha rt s h oul d be c olor ed b y P ro d u c t _Ca t eg ory w hich s h oul d a lso be t he l ege n d. The proport i on s h oul d be ba s ed on num be r o f pur cha s e s , not on t he pur cha s e a mount. The proport i on i s a v a lu e be tw ee n 0 a n d 1 a n d i s not e qu a l to t he num be r o f pur cha s e s (3 points)

library(ggplot2) proportions <- ggplot(sales_data, aes(x = Store_Location, fill = Product_Category)) + geom_bar(position = "fill") + #fill = percentage labs(x = "Store Location", y = "Purchases", title = "Purchase Proportions by Store Location") + guides(x = guide_axis(n.dodge = 2 ))

In [69]:

proportions

2 b) Wha t P ro d u c t _Ca t eg ory acc ounts f or t he low e st c umul a t i v e pur cha s e a mount ( us e v a lu e s i n "P ur cha s e_A mount ")? (1 point)

lowest_product <- aggregate(Purchase_Amount ~ Product_Category, data = sales_data, FUN = sum) lowest_product print(min(lowest_product $ Purchase_Amount))

A data.frame: 6 × 2 Product_Category Purchase_Amount

Books 185993.

Clothing 167241.

Electronics 168118.

Furniture 122478.

Home Appliances 180004.

Toys 168765.

[1] 122478.

Response: Furniture has the lowest cumulative purchase amount.

In [86]:

store_sales <- sales_data %>% group_by(Store_Location) %>% summarise(Total_Sales = sum(Purchase_Amount, na.rm = TRUE )) store_sales which.min(store_sales $ Total_Sales)

A tibble: 13 × 2 Store_Location Total_Sales

Atlanta 56866.

Boston 99292.

Chicago 63450.

Dallas 88589.

Denver 59561.

Houston 64143.

Las Vegas 80970.

Los Angeles 90356.

Miami 74986.

New York 81236.

San Diego 69391.

San Francisco 82676.

Seattle 81080.

1

Response: Atlanta has the lowest total purchases by purchase amount.

2 e) U n f ortun a t e ly , l e t ' s i m agi n e t ha t m a n age m e nt ha s decided to c los e t he De nv e r stor e lo ca t i on a n d a lso stop s e ll i n g f urn i tur e ac ross a ll stor e lo ca t i ons. C r ea t e a n e w da t af r a m e ca ll ed "fi lt e r ed_ s a l e s _da t a", w he r e rows t ha t c ont ai n "De nv e r " i n "S tor e_L o ca t i on " or "F urn i tur e" i n "P ro d u c t _Ca t eg ory " a r e r e mov ed. The r e sult i n g da t af r a m e must ha v e 812 o b s e rv a t i on rows a n d 8 v a r iab l e s. S ort fi lt e r ed_ s a l e s _da t a b y C ustom e r _ID i n de s ce n di n g or de r a n d pr i nt rows 150 to 160 o f t he r e sult i n g da t af r a m e. (3 points)

filtered_sales_data <- sales_data[ ! (sales_data $ Store_Location == "Denver" | sales_data $ Product_Category == "Furniture"), ]

filtered_sales_data <- filtered_sales_data[order(

In [106…

In [113…

filtered_sales_data $ Customer_ID, decreasing = TRUE ),] filtered_sales_data[ 150 : 160 , ]

A data.frame: 11 × 8 Customer_ID Product_Category Purchase_Amount Purchase_Date Customer_Age

817 920 Electronics 1044.86 2024-01-12 30

816 919 Toys 39.67 2024-05-14 40

814 918 Home Appliances 1554.56 2024-03-21 40

810 916 Books 41.64 2024-05-28 27

813 916 Toys 848.26 2024-07-24 27

811 916 Electronics 10.16 2024-07-18 27

808 915 Home Appliances 191.35 2024-07-16 26

809 915 Books 1613.36 2024-05-28 26

807 913 Books 1227.82 2024-05-24 46

806 911 Books 638.48 2024-11-25 63

805 907 Electronics 514.34 2024-10-27 39

2 f) Af t e r fi lt e r i n g out De nv e r a n d F urn i tur e pur cha s e s , c omp a r e t he m ea n s a t i s fac t i on s c or e i n t he fi lt e r ed da t a s e t w i t h t he or igi n a l da t a s e t. Ha s t he r e mov a l o f t he s e rows not iceab ly cha n ged t he a v e r age c ustom e r s a t i s fac t i on? (1 point)

mean(sales_data $ Satisfaction_Score) mean(filtered_sales_data $ Satisfaction_Score)

Response: The removal of these rows has not noticeably changed the average customer satisfaction score. This means that the Denver and Furniture purchases also had a mean score that was close to the original mean.

Question 3: Exploratory Data Analysis (

points)

We w i ll be us i n g t he spl i t da t a s e ts , " tr ai n _ s a l e s _da t a" a n d " t e st _ s a l e s _da t a," c r ea t i n g i n t he st a rt e r c o de i n t he begi nn i n g o f t hi s d o c um e nt g o i n g f orw a r d.

3 a) U s i n g " tr ai n _ s a l e s _da t a", c r ea t e a n He x bi n P lot o f t he r e spons e, "Sa t i s fac t i on _Sc or e" agai nst "P ur cha s e_A mount ". Ca l c ul a t e t he n output t he R-

In [114…

mont h s? N ot e: w e w i ll now ha v e 9 v a r iab l e s ( s i n ce w e added P ur cha s e_M ont h) i n t he r e sult i n g tr ai n a n d t e st da t af r a m e s (3 points)

library(lubridate)

train_sales_data $ Purchase_Month <- as.factor( month(train_sales_data $ Purchase_Date)) #as factor to make months categor

month_boxplot <- ggplot(train_sales_data, aes(x = Purchase_Month, y = Satisfaction_Score)) + geom_boxplot() + labs(title = "Satisfaction Score by Purchase Month")

month_boxplot

Response: August seems to have the highest median satisfaction score. There is one outlier in October with a score of 1. Otherwise, none of the median satisfaction scores are that different from each other.

3 c) If on e mont h a pp ea r ed to ha v e a mu ch highe r m edia n s a t i s fac t i on t ha n ot he rs , h ow woul d you f orm a lly t e st t hi s diffe r e n ce? (1 point)

Response: I would use the median() function to find the exact median satisfaction score of each month. I could also run an ANOVA test to compare the means of each month to see if they are significantly different.

In [132…

Question 4: Multiple Linear Regression (

points)

4 a) U s i n g tr ai n _ s a l e s _da t a, c r ea t e a mult i pl e l i n ea r r eg r e ss i on mo de l a n d ca ll i t " mo de l 1" w i t h Sa t i s fac t i on _Sc or e a s t he r e spons e t he n P ro d u c t _Ca t eg ory , P ur cha s e_A mount , C ustom e r _Age, C ustom e r _Ge n de r , S tor e_L o ca t i on , a n d P ur cha s e_M ont h a s t he pr edic tors. P r i nt t he mo de l summ a ry. I s t he mo de l o f a ny us e i n pr edic t i n g Sa t i s fac t i on _Sc or e? us i n g α = 0.05, prov ide t he f ollow i n g e l e m e nts o f t he t e st o f ov e r a ll r eg r e ss i on o f t he mo de l : null h ypot he s i s H0, a lt e rn a t i v e h ypot he s i s Ha, F- st a t i st ic or p - v a lu e, a n d c on c lus i on (3 points)

model1 <- lm(Satisfaction_Score ~ Product_Category + Purchase_Amount + Customer_Age + Customer_Gender + Store_Location + Purchase_Month, data = train_sales_data) summary(model1)

In [146…

of the predictors has a significant linear relationship with satisfaction score. Ha: At least one of the predictor variables has a significant relationship with satisfaction score. The p-value is < 2.2e-16 with an F-statistic of 33.34. This p-value is less than the significance level 0.05. We can conclude that at least one of the predictors has a significant relationship with satisfaction score.

4 b) U s i n g α = 0.05, w hich o f t he e st i m a t ed c o efficie nts a r e st a t i st ica lly s ig n ifica nt i n mo de l 1 (1 points)

Response: Product_CategoryElectronics, Product_CategoryFurniture, Product_CategoryHome Appliances, Product_CategoryToys, and Purchase_Amount, are statistically significant.

4 c) Wha t i s t he i nt e rpr e t a t i on o f t he i nt e r ce pt c o efficie nt w i t h r e sp ec t to t he s a t i s fac t i on s c or e? I n ot he r wor d s , w ha t d o e s t he i nt e r ce pt r e pr e s e nt f or t hi s p a rt ic ul a r mo de l (2 points)

Response: The intercept represents the estimated satisfaction score when all variables are held at their baseline values. In this model, the satisfaction score is expected to be 6.180 when the product category = books, purchase amount = 0, customer age = 0, customer gender = female, store location = Atlanta, and purchase month = January.

4 d) Wha t i s t he i nt e rpr e t a t i on o f t he P ur cha s e_A mount c o efficie nt? (1 points)

Response: The satisfaction score will increase by 1.031e-03 when Purchase Amount increases by $1, holding all other variables constant.

4 e) C r ea t e s ca tt e r plots o f t he st a n da r di z ed r e s id u a ls o f mo de l 1 vs. t he qu a nt i t a t i v e pr edic tors (P ur cha s e_A mount , C ustom e r _Age). U s i n g t he s e plots , d o e s t he l i n ea r i ty a ssumpt i on h ol d f or t he s e pr edic tors? (2 points)

plot(train_sales_data $ Purchase_Amount, resid(model1), main = "Residuals vs Purchase amount", xlab = "Purchase amount", ylab = "Standardized residuals")

plot(train_sales_data $ Customer_Age, resid(model1), main = "Residuals vs Customer Age", xlab = "Purchase amount", ylab = "Standardized residuals")

In [153…

Response: For both variables, the linearity assumption holds because there is no pattern

in the scatter plot of the variable vs residuals.

Warning message: “package ‘car’ was built under R version 4.1.2” Loading required package: carData

Warning message: “package ‘carData’ was built under R version 4.1.2”

Attaching package: ‘car’

The following object is masked from ‘package:dplyr’:

recode

Response: The QQ plot shows a relatively straight line, and the histogram shows an approximately normal distribution. So, we can assume normality of the errors.

5) Multi-collinearity and Outliers (9 points)

5 a) Ca l c ul a t e t he VIF o f each pr edic tor i n mo de l 1. U s i n g a VIF t h r e s h ol d o f w ha t c on c lus i on d o you m ake r ega r di n g mult ic ol i n ea r i ty? ( points)

round(vif(model1), 3 )

A matrix: 6 × 3 of type dbl GVIF Df GVIF^(1/(2Df))*

Product_Category 1.195 5 1.

Purchase_Amount 1.052 1 1.

Customer_Age 1.038 1 1.

Customer_Gender 1.030 1 1.

Store_Location 1.322 12 1.

Purchase_Month 1.292 11 1.

max (10, 1/(1 − R^2 ))

In [165…

Response: There are 5 peaks that are higher than the others, so there seems to be 5 outliers.

5 c) C r ea t e a n e w da t af r a m e f rom tr ai n _ s a l e s _da t a a n d ca ll i t tr ai n _ s a l e s _da t a_c oo k. Thi s da t af r a m e s h oul d e x c lu de t he outl ie rs ide nt ified i n Q u e st i on (5b) ab ov e. S ort t he da t af r a m e b y P ur cha s e_A mount i n de s ce n di n g or de r a n d pr i nt t he fi rst 5 rows. N ot e: t he sort i n g s h oul d be p e rm a n e ntly ( not t e mpor a r i ly ) stor ed unl e ss ot he rw i s e st a t ed. (2 points)

train_sales_data_cook <- train_sales_data[ - outliers,] train_sales_data_cook <- train_sales_data_cook[order( train_sales_data_cook $ Purchase_Amount, decreasing = T),] head(train_sales_data_cook, 5 )

A data.frame: 5 × Customer_ID Product_Category Purchase_Amount Purchase_Date Customer_Age

792 326 Electronics 1990.98 2024-11-11 28

406 153 Clothing 1986.71 2024-11-06 19

449 759 Books 1984.87 2024-05-31 31

279 804 Books 1983.22 2024-05-23 35

745 1015 Home Appliances 1982.30 2024-03-18 64

In [181…

6) Subsets of Coefficients (6 points)

6 a) Ca l c ul a t e t he 95 % c on fide n ce i nt e rv a l f or t he r eg r e ss i on c o efficie nt o f C ustom e r _Age i n mo de l 1. Wha t ca n you c on c lu de f rom t he 95 % c on fide n ce i nt e rv a l f or t he C ustom e r _Age c o efficie nt? (1 point)

confint(model1, "Customer_Age",level =. 95 )

A matrix: 1 × 2 of type dbl 2.5 % 97.5 %

Customer_Age -0.006521375 0.

Response: The interval includes 0, so the coefficient is insignficant. Customer age is not a significant predictor for satisfaction.

6 b) U s i n g tr ai n _ s a l e s _da t a, c r ea t e a n e w mo de l a n d ca ll i t “ mo de l _ no _age” w i t h t he s a m e r e spons e a n d pr edic tors a s “ mo de l 1”, b ut w i t h out t he pr edic tor C ustom e r _Age. P r i nt t he mo de l summ a ry. (1 point)

model_no_age <- lm(Satisfaction_Score ~ Product_Category + Purchase_Amount + Customer_Gender + Store_Location + Purchase_Month, data = train_sales_data) summary(model_no_age)

In [182…

In [183…