#install.packages("dataset")
library(datasets)
mydata <- (mtcars)
head(mydata)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Explanation of data set
I would like to delete variables disp, drat, vs, and carb variable as I do not know what they mechanically represent in a car, and by that creaing a new data set
mydata2 <- mydata[ , -3 ][ , -4] [ , -6] [ , -8]
And I am going to make a new variable, called hpNew, with decreased number of gross horsepower of each car by 5
mydata2$hpNew <- mydata2$hp + 5
print(mydata2)
## mpg cyl hp wt qsec am gear hpNew
## Mazda RX4 21.0 6 110 2.620 16.46 1 4 115
## Mazda RX4 Wag 21.0 6 110 2.875 17.02 1 4 115
## Datsun 710 22.8 4 93 2.320 18.61 1 4 98
## Hornet 4 Drive 21.4 6 110 3.215 19.44 0 3 115
## Hornet Sportabout 18.7 8 175 3.440 17.02 0 3 180
## Valiant 18.1 6 105 3.460 20.22 0 3 110
## Duster 360 14.3 8 245 3.570 15.84 0 3 250
## Merc 240D 24.4 4 62 3.190 20.00 0 4 67
## Merc 230 22.8 4 95 3.150 22.90 0 4 100
## Merc 280 19.2 6 123 3.440 18.30 0 4 128
## Merc 280C 17.8 6 123 3.440 18.90 0 4 128
## Merc 450SE 16.4 8 180 4.070 17.40 0 3 185
## Merc 450SL 17.3 8 180 3.730 17.60 0 3 185
## Merc 450SLC 15.2 8 180 3.780 18.00 0 3 185
## Cadillac Fleetwood 10.4 8 205 5.250 17.98 0 3 210
## Lincoln Continental 10.4 8 215 5.424 17.82 0 3 220
## Chrysler Imperial 14.7 8 230 5.345 17.42 0 3 235
## Fiat 128 32.4 4 66 2.200 19.47 1 4 71
## Honda Civic 30.4 4 52 1.615 18.52 1 4 57
## Toyota Corolla 33.9 4 65 1.835 19.90 1 4 70
## Toyota Corona 21.5 4 97 2.465 20.01 0 3 102
## Dodge Challenger 15.5 8 150 3.520 16.87 0 3 155
## AMC Javelin 15.2 8 150 3.435 17.30 0 3 155
## Camaro Z28 13.3 8 245 3.840 15.41 0 3 250
## Pontiac Firebird 19.2 8 175 3.845 17.05 0 3 180
## Fiat X1-9 27.3 4 66 1.935 18.90 1 4 71
## Porsche 914-2 26.0 4 91 2.140 16.70 1 5 96
## Lotus Europa 30.4 4 113 1.513 16.90 1 5 118
## Ford Pantera L 15.8 8 264 3.170 14.50 1 5 269
## Ferrari Dino 19.7 6 175 2.770 15.50 1 5 180
## Maserati Bora 15.0 8 335 3.570 14.60 1 5 340
## Volvo 142E 21.4 4 109 2.780 18.60 1 4 114
Now I am going to change the names of the columns
colnames(mydata2) <- c("Miles/Gallon", "Cylinders", "Horsepower", "Weight", "1/4 Mile Time", "Transmission" , "Gears" , "NewHorsepower")
head(mydata2)
## Miles/Gallon Cylinders Horsepower Weight 1/4 Mile Time
## Mazda RX4 21.0 6 110 2.620 16.46
## Mazda RX4 Wag 21.0 6 110 2.875 17.02
## Datsun 710 22.8 4 93 2.320 18.61
## Hornet 4 Drive 21.4 6 110 3.215 19.44
## Hornet Sportabout 18.7 8 175 3.440 17.02
## Valiant 18.1 6 105 3.460 20.22
## Transmission Gears NewHorsepower
## Mazda RX4 1 4 115
## Mazda RX4 Wag 1 4 115
## Datsun 710 1 4 98
## Hornet 4 Drive 0 3 115
## Hornet Sportabout 0 3 180
## Valiant 0 3 110
I want to change categorical variable, which is coded in numbers (Transmission)
mydata2$Transmission <- factor(mydata2$Transmission,
levels = c(0,1),
labels = c("Automatic" , "Manual"))
head(mydata2)
## Miles/Gallon Cylinders Horsepower Weight 1/4 Mile Time
## Mazda RX4 21.0 6 110 2.620 16.46
## Mazda RX4 Wag 21.0 6 110 2.875 17.02
## Datsun 710 22.8 4 93 2.320 18.61
## Hornet 4 Drive 21.4 6 110 3.215 19.44
## Hornet Sportabout 18.7 8 175 3.440 17.02
## Valiant 18.1 6 105 3.460 20.22
## Transmission Gears NewHorsepower
## Mazda RX4 Manual 4 115
## Mazda RX4 Wag Manual 4 115
## Datsun 710 Manual 4 98
## Hornet 4 Drive Automatic 3 115
## Hornet Sportabout Automatic 3 180
## Valiant Automatic 3 110
I will make mydata3, which only includes automatic transmission cars with NewHorsepower higher than 100
mydata3 <- mydata2[mydata2$Transmission == "Automatic" & mydata2$NewHorsepower >=100 , ]
head(mydata3)
## Miles/Gallon Cylinders Horsepower Weight 1/4 Mile Time
## Hornet 4 Drive 21.4 6 110 3.215 19.44
## Hornet Sportabout 18.7 8 175 3.440 17.02
## Valiant 18.1 6 105 3.460 20.22
## Duster 360 14.3 8 245 3.570 15.84
## Merc 230 22.8 4 95 3.150 22.90
## Merc 280 19.2 6 123 3.440 18.30
## Transmission Gears NewHorsepower
## Hornet 4 Drive Automatic 3 115
## Hornet Sportabout Automatic 3 180
## Valiant Automatic 3 110
## Duster 360 Automatic 3 250
## Merc 230 Automatic 4 100
## Merc 280 Automatic 4 128
I am going to check mydata3 and delete units, if there is any missing data in its variables
library(tidyr)
mydata4 <- drop_na(mydata3)
head(mydata4)
## Miles/Gallon Cylinders Horsepower Weight 1/4 Mile Time
## Hornet 4 Drive 21.4 6 110 3.215 19.44
## Hornet Sportabout 18.7 8 175 3.440 17.02
## Valiant 18.1 6 105 3.460 20.22
## Duster 360 14.3 8 245 3.570 15.84
## Merc 230 22.8 4 95 3.150 22.90
## Merc 280 19.2 6 123 3.440 18.30
## Transmission Gears NewHorsepower
## Hornet 4 Drive Automatic 3 115
## Hornet Sportabout Automatic 3 180
## Valiant Automatic 3 110
## Duster 360 Automatic 3 250
## Merc 230 Automatic 4 100
## Merc 280 Automatic 4 128
summary(mydata4[,c(-6)])
## Miles/Gallon Cylinders Horsepower Weight
## Min. :10.40 Min. :4.000 Min. : 95.0 Min. :2.465
## 1st Qu.:14.82 1st Qu.:6.000 1st Qu.:123.0 1st Qu.:3.440
## Median :16.85 Median :8.000 Median :175.0 Median :3.545
## Mean :16.74 Mean :7.111 Mean :165.7 Mean :3.801
## 3rd Qu.:19.07 3rd Qu.:8.000 3rd Qu.:198.8 3rd Qu.:3.844
## Max. :22.80 Max. :8.000 Max. :245.0 Max. :5.424
## 1/4 Mile Time Gears NewHorsepower
## Min. :15.41 Min. :3.000 Min. :100.0
## 1st Qu.:17.11 1st Qu.:3.000 1st Qu.:128.0
## Median :17.71 Median :3.000 Median :180.0
## Mean :18.08 Mean :3.167 Mean :170.7
## 3rd Qu.:18.75 3rd Qu.:3.000 3rd Qu.:203.8
## Max. :22.90 Max. :4.000 Max. :250.0
library(pastecs)
##
## Attaching package: 'pastecs'
## The following object is masked from 'package:tidyr':
##
## extract
stat.desc(mydata4 [ ,c(-6)])
## Miles/Gallon Cylinders Horsepower Weight 1/4 Mile Time
## nbr.val 18.0000000 18.0000000 18.0000000 18.0000000 18.00000000
## nbr.null 0.0000000 0.0000000 0.0000000 0.0000000 0.00000000
## nbr.na 0.0000000 0.0000000 0.0000000 0.0000000 0.00000000
## min 10.4000000 4.0000000 95.0000000 2.4650000 15.41000000
## max 22.8000000 8.0000000 245.0000000 5.4240000 22.90000000
## range 12.4000000 4.0000000 150.0000000 2.9590000 7.49000000
## sum 301.4000000 128.0000000 2983.0000000 68.4190000 325.48000000
## median 16.8500000 8.0000000 175.0000000 3.5450000 17.71000000
## mean 16.7444444 7.1111111 165.7222222 3.8010556 18.08222222
## SE.mean 0.8265695 0.3322422 11.7319756 0.1854563 0.41113231
## CI.mean.0.95 1.7439092 0.7009698 24.7523049 0.3912787 0.86741335
## var 12.2979085 1.9869281 2477.5065359 0.6190930 3.04253595
## std.dev 3.5068374 1.4095844 49.7745571 0.7868246 1.74428666
## coef.var 0.2094329 0.1982228 0.3003493 0.2070016 0.09646418
## Gears NewHorsepower
## nbr.val 18.00000000 18.0000000
## nbr.null 0.00000000 0.0000000
## nbr.na 0.00000000 0.0000000
## min 3.00000000 100.0000000
## max 4.00000000 250.0000000
## range 1.00000000 150.0000000
## sum 57.00000000 3073.0000000
## median 3.00000000 180.0000000
## mean 3.16666667 170.7222222
## SE.mean 0.09038769 11.7319756
## CI.mean.0.95 0.19070136 24.7523049
## var 0.14705882 2477.5065359
## std.dev 0.38348249 49.7745571
## coef.var 0.12109974 0.2915529
round(stat.desc(mydata4 [ ,c(-6)]) , 1)
## Miles/Gallon Cylinders Horsepower Weight 1/4 Mile Time Gears
## nbr.val 18.0 18.0 18.0 18.0 18.0 18.0
## nbr.null 0.0 0.0 0.0 0.0 0.0 0.0
## nbr.na 0.0 0.0 0.0 0.0 0.0 0.0
## min 10.4 4.0 95.0 2.5 15.4 3.0
## max 22.8 8.0 245.0 5.4 22.9 4.0
## range 12.4 4.0 150.0 3.0 7.5 1.0
## sum 301.4 128.0 2983.0 68.4 325.5 57.0
## median 16.9 8.0 175.0 3.5 17.7 3.0
## mean 16.7 7.1 165.7 3.8 18.1 3.2
## SE.mean 0.8 0.3 11.7 0.2 0.4 0.1
## CI.mean.0.95 1.7 0.7 24.8 0.4 0.9 0.2
## var 12.3 2.0 2477.5 0.6 3.0 0.1
## std.dev 3.5 1.4 49.8 0.8 1.7 0.4
## coef.var 0.2 0.2 0.3 0.2 0.1 0.1
## NewHorsepower
## nbr.val 18.0
## nbr.null 0.0
## nbr.na 0.0
## min 100.0
## max 250.0
## range 150.0
## sum 3073.0
## median 180.0
## mean 170.7
## SE.mean 11.7
## CI.mean.0.95 24.8
## var 2477.5
## std.dev 49.8
## coef.var 0.3
hist(mydata4$`1/4 Mile Time`,
main = "Distribution of cars acceleration",
xlab = "1/4 mile time [s]",
ylab = "Frequency" ,
breaks = seq(from =14, to = 24, by= 1))
hist(mydata4$Horsepower,
main = "Distribution of cars Horsepower",
xlab = "Horsepower",
ylab = "Frequency" ,
breaks = seq(from =80, to = 270, by= 30))
#install.packages("car")
library(car)
## Loading required package: carData
scatterplot(y = mydata4$`Miles/Gallon`,
x = mydata4$Horsepower,
col = "green",
ylab = "Miles/(US) gallon)",
xlab = "Gross horsepower",
smooth = FALSE)
I want to check how does different transmission variable from mydata2 set affect the graph with the same x and y values as before
library(car)
scatterplot(`Miles/Gallon` ~ Horsepower | Transmission,
ylab = "Miles/(US) gallon)",
xlab = "Gross horsepower",
smooth = FALSE,
data=mydata2)
data <- read.csv("~/IMB/PYTHON exercises/Bootcamp23_working/Task 2/Body mass.csv",
header = TRUE,
sep = ";",
dec = ",")
head(data)
## ID Mass
## 1 1 62.1
## 2 2 64.5
## 3 3 56.5
## 4 4 53.4
## 5 5 61.3
## 6 6 62.2
I am going to show descriptive statistics of numeric variable
summary(data$Mass)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 49.70 60.23 62.80 62.88 64.50 83.20
library(pastecs)
round(stat.desc(data [ ,-1]) , 2)
## nbr.val nbr.null nbr.na min max range
## 50.00 0.00 0.00 49.70 83.20 33.50
## sum median mean SE.mean CI.mean.0.95 var
## 3143.80 62.80 62.88 0.85 1.71 36.14
## std.dev coef.var
## 6.01 0.10
hist(data$Mass,
main = "Distribution of body weight among ninth graders",
xlab = "Body weight",
ylab = "Frequency" ,
col = "lightskyblue1",
breaks = seq(from =40, to = 90, by= 10))
I am going to test the hypothesis of 𝐻0: 𝜇 = 59.5, average from 2018/2019 data
t.test(data$Mass,
mu = 59.5,
alternative = "two.sided")
##
## One Sample t-test
##
## data: data$Mass
## t = 3.9711, df = 49, p-value = 0.000234
## alternative hypothesis: true mean is not equal to 59.5
## 95 percent confidence interval:
## 61.16758 64.58442
## sample estimates:
## mean of x
## 62.876
Because p-value= 0.000234,less than 0.001, which is lower than alpha value of 0.05, we reject 𝐻0 and accept H1, so the average body weight of ninth graders in 2021/2022 is NOT the same as of ninth graders in 2018/2019, at p<0.001
Arithmetic mean of 2021/2022: = 62.88
Average body weight of ninth graders in 2021/2022 is different than as of ninth graders in 2018/2019
95% CI for arithmetic mean: 61.16758 < Mu < 64.58442
Because 59.5 is not included in the interval, we reject H0 at p<0.001
Calculating the effect size
#install.packages("effectsize")
library(effectsize)
cohens_d(data$Mass, mu = 59.5)
## Cohen's d | 95% CI
## ------------------------
## 0.56 | [0.26, 0.86]
##
## - Deviation from a difference of 59.5.
Interpret the effect size
interpret_cohens_d(0.56 , rules = "sawilowsky2009")
## [1] "medium"
## (Rules: sawilowsky2009)
#install.packages("readxl")
library(readxl)
my3data <- read_xlsx("~/IMB/PYTHON exercises/Bootcamp23_working/Task 3/Apartments.xlsx")
head(my3data)
## # A tibble: 6 × 5
## Age Distance Price Parking Balcony
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 7 28 1640 0 1
## 2 18 1 2800 1 0
## 3 7 28 1660 0 0
## 4 28 29 1850 0 1
## 5 18 18 1640 1 1
## 6 28 12 1770 0 1
Description:
my3data$Balcony <- factor(my3data$Balcony,
levels = c(0,1),
labels = c("No" , "Yes"))
my3data$Parking <- factor(my3data$Parking,
levels = c(0,1),
labels = c("No" , "Yes"))
head(my3data)
## # A tibble: 6 × 5
## Age Distance Price Parking Balcony
## <dbl> <dbl> <dbl> <fct> <fct>
## 1 7 28 1640 No Yes
## 2 18 1 2800 Yes No
## 3 7 28 1660 No No
## 4 28 29 1850 No Yes
## 5 18 18 1640 Yes Yes
## 6 28 12 1770 No Yes
t.test(my3data$Price,
mu = 1900,
alternative = "two.sided")
##
## One Sample t-test
##
## data: my3data$Price
## t = 2.9022, df = 84, p-value = 0.004731
## alternative hypothesis: true mean is not equal to 1900
## 95 percent confidence interval:
## 1937.443 2100.440
## sample estimates:
## mean of x
## 2018.941
p-value: 0.004731, less than 0.05
p-value is less than 0.05, We reject H0, at p<0.001, and accept H1. Average prices is different than 1900 eur
95% CI for arithmetic mean: 1937.4 < Mu < 2100.4
Because 1900 is not included in the interval, we reject H0 at p<0.001
scatterplot(my3data$Price ~ my3data$Age,
smooth = FALSE,
boxplots = FALSE,
ylab = "Price per m2 in EUR",
xlab = "Age of an apartment in years")
fit1 <- lm(Price ~ Age,
data = my3data)
summary(fit1)
##
## Call:
## lm(formula = Price ~ Age, data = my3data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -623.9 -278.0 -69.8 243.5 776.1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2185.455 87.043 25.108 <2e-16 ***
## Age -8.975 4.164 -2.156 0.034 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 369.9 on 83 degrees of freedom
## Multiple R-squared: 0.05302, Adjusted R-squared: 0.04161
## F-statistic: 4.647 on 1 and 83 DF, p-value: 0.03401
cor(my3data$Price, my3data$Age)
## [1] -0.230255
library(car)
scatterplotMatrix(my3data[ , c(-4, -5)],
smooth = FALSE)
fit2 <- lm(Price ~ Age + Distance,
data = my3data)
summary(fit2)
##
## Call:
## lm(formula = Price ~ Age + Distance, data = my3data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -603.23 -219.94 -85.68 211.31 689.58
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2460.101 76.632 32.10 < 2e-16 ***
## Age -7.934 3.225 -2.46 0.016 *
## Distance -20.667 2.748 -7.52 6.18e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 286.3 on 82 degrees of freedom
## Multiple R-squared: 0.4396, Adjusted R-squared: 0.4259
## F-statistic: 32.16 on 2 and 82 DF, p-value: 4.896e-11
vif(fit2)
## Age Distance
## 1.001845 1.001845
my3data$StdResid <- round(rstandard(fit2), 3) #calculating Stand. residuals
my3data$CooksD <- round(cooks.distance(fit2), 3) #Cooks distances
head(my3data[order(-my3data$StdResid),], 5)
## # A tibble: 5 × 7
## Age Distance Price Parking Balcony StdResid CooksD
## <dbl> <dbl> <dbl> <fct> <fct> <dbl> <dbl>
## 1 5 45 2180 Yes Yes 2.58 0.32
## 2 2 11 2790 Yes No 2.05 0.069
## 3 18 1 2800 Yes No 1.78 0.03
## 4 18 1 2800 Yes Yes 1.78 0.03
## 5 8 2 2820 Yes No 1.66 0.037
head(my3data[order(my3data$StdResid),], 5) #Five units with lowest and value of stand. residuals
## # A tibble: 5 × 7
## Age Distance Price Parking Balcony StdResid CooksD
## <dbl> <dbl> <dbl> <fct> <fct> <dbl> <dbl>
## 1 7 2 1760 No Yes -2.15 0.066
## 2 12 14 1650 No Yes -1.50 0.013
## 3 12 14 1650 No No -1.50 0.013
## 4 13 8 1800 No No -1.38 0.012
## 5 14 16 1660 No Yes -1.26 0.008
head(my3data[order(-my3data$CooksD),], 6) #Six units with highest value of Cooks distance
## # A tibble: 6 × 7
## Age Distance Price Parking Balcony StdResid CooksD
## <dbl> <dbl> <dbl> <fct> <fct> <dbl> <dbl>
## 1 5 45 2180 Yes Yes 2.58 0.32
## 2 43 37 1740 No No 1.44 0.104
## 3 2 11 2790 Yes No 2.05 0.069
## 4 7 2 1760 No Yes -2.15 0.066
## 5 37 3 2540 Yes Yes 1.58 0.061
## 6 40 2 2400 No Yes 1.09 0.038
hist(my3data$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distances")
- Because the unit (apartment, in row 38, when I look in my3data) with
the highest StdResid (2.577) and highest CooksD (0.320) is the same, and
it is the extreme value on the right when I check the Histogram of Cooks
distances, I am going to remove it
my4data <- my3data[-38, ]
fit2 <- lm(Price ~ Age + Distance,
data = my4data) #Removing unit and adjusting linear regression model
my4data$StdFitted <- scale(fit2$fitted.values)
library(car)
scatterplot(y = my4data$StdResid, x = my4data$StdFitted,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
library(olsrr)
##
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
##
## rivers
ols_test_breusch_pagan(fit2)
##
## Breusch Pagan Test for Heteroskedasticity
## -----------------------------------------
## Ho: the variance is constant
## Ha: the variance is not constant
##
## Data
## ---------------------------------
## Response : Price
## Variables: fitted values of Price
##
## Test Summary
## -----------------------------
## DF = 1
## Chi2 = 2.927455
## Prob > Chi2 = 0.08708469
H0: the variance is constant
H1: the variance is NOT constant
p = 0.087
We cannot reject H0 (p> 0.05),that means we assume the variance is constant, so there is no heteroskedasticity, therefore we assume the variability of error terms is constant.
hist(my4data$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals")
shapiro.test(my4data$StdResid)
##
## Shapiro-Wilk normality test
##
## data: my4data$StdResid
## W = 0.94879, p-value = 0.002187
fit2 <- lm(Price ~ Age + Distance,
data = my3data)
summary(fit2)
##
## Call:
## lm(formula = Price ~ Age + Distance, data = my3data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -603.23 -219.94 -85.68 211.31 689.58
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2460.101 76.632 32.10 < 2e-16 ***
## Age -7.934 3.225 -2.46 0.016 *
## Distance -20.667 2.748 -7.52 6.18e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 286.3 on 82 degrees of freedom
## Multiple R-squared: 0.4396, Adjusted R-squared: 0.4259
## F-statistic: 32.16 on 2 and 82 DF, p-value: 4.896e-11
sqrt(summary(fit2)$r.squared)
## [1] 0.6629887
Price = 2460.101-7.934 Age - 20.667 Distance
Beta1: -7.934
beta2: -20.667
H0: beta1 = 0 H1: beta1 != 0 p = 0.016
Explanation of -7.934 (estimate of the true parameter, beta 1) If age of the apartment increases by one year, price of the apartment is decreased on average by 7.934 EUR per m2 (p < 0.05), assuming that all other explanatory variables, included in the model, are constant (but in this specific regression model right now, there is no other variables).
H0: beta2 = 0 H1: beta2 != 0 p=6.18e-11
Explanation of -20.667 (estimate of the true parameter, beta 2) If the distance from city center increases by 1km, price of the apartment decreases on average by 20.667 EUR per m2 (p < 0.05), assuming that all other explanatory variables, included in the model, are constant (but in this specific regression model right now, there is no other variables).
Multiple R2: 0.4396, 43.96% of variability of price of the apartment is explained by linear effect of Age and Distance of the apartment.
Test of significance regression: H0: Ro2 = 0 H1: Ro2 > 0
F-stat = 32.16, p< 0.001
We reject H0, and accept H1, at p <0.001, we can say that population coefficient of determination is more than 0, meaning we found the linear relationship between price of the apartment and explanatory variables (age, distance) included in the model.
Multiple correlation coefficient =0.6629887, indicates a moderately strong positive linear relationship between price of the apartment and all explanatory variables included in the model (age, distance).
my3data$BalconyB <- factor(my3data$Balcony,
levels = c("No", "Yes"),
labels = c("No Balcony", "Has balcony"))
my3data$ParkingP <- factor(my3data$Parking,
levels = c("No", "Yes"),
labels = c("No Parking", "Has parking"))
fit3 <- lm(Price ~ Age + Distance + ParkingP + BalconyB,
data = my3data)
summary(fit3)
##
## Call:
## lm(formula = Price ~ Age + Distance + ParkingP + BalconyB, data = my3data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -459.92 -200.66 -57.48 260.08 594.37
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2301.667 94.271 24.415 < 2e-16 ***
## Age -6.799 3.110 -2.186 0.03172 *
## Distance -18.045 2.758 -6.543 5.28e-09 ***
## ParkingPHas parking 196.168 62.868 3.120 0.00251 **
## BalconyBHas balcony 1.935 60.014 0.032 0.97436
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 273.7 on 80 degrees of freedom
## Multiple R-squared: 0.5004, Adjusted R-squared: 0.4754
## F-statistic: 20.03 on 4 and 80 DF, p-value: 1.849e-11
anova(fit2, fit3)
## Analysis of Variance Table
##
## Model 1: Price ~ Age + Distance
## Model 2: Price ~ Age + Distance + ParkingP + BalconyB
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 82 6720983
## 2 80 5991088 2 729894 4.8732 0.01007 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
H0: model 2 is better
H1: model 3 is better
p= 0.01007 <<<< we reject H0 at p<0.05, model 3 is better, meaning it explains more variability in price of the apartment
summary(fit3)
##
## Call:
## lm(formula = Price ~ Age + Distance + ParkingP + BalconyB, data = my3data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -459.92 -200.66 -57.48 260.08 594.37
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2301.667 94.271 24.415 < 2e-16 ***
## Age -6.799 3.110 -2.186 0.03172 *
## Distance -18.045 2.758 -6.543 5.28e-09 ***
## ParkingPHas parking 196.168 62.868 3.120 0.00251 **
## BalconyBHas balcony 1.935 60.014 0.032 0.97436
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 273.7 on 80 degrees of freedom
## Multiple R-squared: 0.5004, Adjusted R-squared: 0.4754
## F-statistic: 20.03 on 4 and 80 DF, p-value: 1.849e-11
H0: beta3= 0 H1: beta 3!= 0 p=0.00251 <<<< p<0.001 beta3= 196.168, Assuming all other variables are the same, apartments having a parking spot have on average 196.168 EUR per m2 higher price compared to the apartments with no parking (at p<0.001)
H0: beta4= 0 H1: beta4 != 0 p= 0.97436 <<< p>0.05 beta4= 1.935, , Assuming all other variables are the same, apartments having a balcony have on average 196.168 EUR per m2 higher price compared to the apartments with no balcony (at p>0.05)
H0:all regression coefficient in the model (beta1, beta2, beta3, beta4) = 0 H1:at least one regression coefficient in the model (beta1, beta2, beta3, beta4)!= 0 p= 1.849e-11 <<<< p<0.001 We reject H0 and accept H1, which means the regression model is statistically significant, meaning we found the linear relationship between price of the apartment and explanatory variables included in the model, and at least one explanatory variable has a significant effect on the dependent variable (p<0.001)
my3data$Fitted <- fitted.values(fit3)
my3data$Residuals <- residuals(fit3)
head(my3data[colnames(my3data) %in% c("Price", "Fitted", "Residuals")])
## # A tibble: 6 × 3
## Price Fitted Residuals
## <dbl> <dbl> <dbl>
## 1 1640 1751. -111.
## 2 2800 2357. 443.
## 3 1660 1749. -88.8
## 4 1850 1590. 260.
## 5 1640 2053. -413.
## 6 1770 1897. -127.