lab 7_MLRM
Lab 7_MLRM_CAKES.txt
—
Plain Text,
1 kB (1716 bytes)
Contenuto del file
##### UNIFE - MINI - STATISTICA MULTIVARIATA
### MLRM
### research question: do advertising and price affect the total sales of pie?
getwd()
torta<-read.csv2("torta.csv")
# analyze the database
View(torta)
str(torta)
head(torta)
#describe the variables
attach(torta)
torta=torta[,-1]# we deleted week from our in-use database because we are not interested in
# we rename our variables of interest:
x1=Price
x2=Advertising
y=Pie.sales
#1. graphical analysis
plot(x1,y)
plot(x2,y)
pairs(x=torta,panel=panel.smooth)
#2.identify the coefficients of our model
#we may use two methods: glm () or lm() [let's start with lm]
reg=lm(y~x1+x2)
reg
#to identify the coefficients and the method used
summary(reg)
#please, try to explain all the part composing our output
#### OUR MODEL: y= 306.53-24.98*Price+74.13*Advertising
#3. graphical representation (you should have the 3D software)
library(scatterplot3d) # let's check the availability of the 3D
sc=scatterplot3d(y,x1,x2)
#4. Let's make a prediction
#ex. price=5$, advertise=410 $ ( -> the value is 4.1)
b0=306.53
b1=-24.93
b2=74.13
pred=b0+b1*5+b2*4.1
pred #485.813
#5.the goodness of fit of our model
#5A= RQ from the output
#5B= from the ANOVA
#let's go head with the exercises 5a and 5b and compare the obtained values
summary(reg)
anova(reg) #(11100+1836)/(11100+18360+27033)
#5.II - the comparison between different models --> R2 adj.
summary(reg)
#please, try to add/subctract a variable and make a comment
#es. we take off the price (x1)
reg2=lm(y~x2)
summary(reg2)
#es. we take off the advertising (x2)
reg3=lm(y~x1)
summary(reg3)