mydata <- read.csv("TASK 1.csv",
header = TRUE,
sep = ";",
dec = ",") #Importing dataset
head(mydata)
## Rank Country.Region EU.NON.EU Cost.index Ø.Monthly.income
## 1 1 Bermuda * NON EU 157.9 10,437 USD
## 2 2 Cayman Islands * NON EU 138.3 5,433 USD
## 3 3 Switzerland NON EU 131.4 7,454 USD
## 4 4 Turks and Caicos Islands * NON EU 124.6 2,013 USD
## 5 5 Israel NON EU 123.1 4,554 USD
## 6 6 Barbados NON EU 121.8 1,613 USD
## Purchasing.power.index
## 1 103.8
## 2 61.7
## 3 89.1
## 4 25.4
## 5 58.1
## 6 20.8
Rank:This column represents the ranking of countries or regions based on a specific criterion, which is not explicitly mentioned in the column name. The rank could be related to the overall cost of living, quality of life, economic indicators, or any other relevant metric. Lower ranks typically indicate better performance or lower costs, depending on the context.
Country/Region: This column contains the names of the countries or regions being analyzed in the dataset. Each row corresponds to a specific country or region, and this column serves as the identifier for each data point.
EU/NON EU:This column provides the information whether the country is a part of the European Union ir it is not.
Cost index: The cost index column provides a numerical value that quantifies the cost of living in each country or region. A higher cost index value indicates that living expenses, such as housing, food, transportation, and other essentials, are relatively high in that particular location. Conversely, a lower cost index suggests a more affordable cost of living.
Monthly Income: This column represents the average monthly income for residents of each country or region. It provides insight into the earning potential and income levels of the population in different areas. A higher monthly income typically suggests greater financial resources for residents.
Purchasing Power Index: The purchasing power index column measures the relative purchasing power of the population in each country or region. It indicates how far a given income will go in terms of buying goods and services in that location. A higher purchasing power index suggests that the population can afford a relatively higher standard of living, while a lower index implies reduced purchasing power.
colnames(mydata) <- c("Rank", "Country", "EU MemberNon EU Member", "Cost Index", "Monthly Income", "PPI")
head(mydata)
## Rank Country EU MemberNon EU Member Cost Index
## 1 1 Bermuda * NON EU 157.9
## 2 2 Cayman Islands * NON EU 138.3
## 3 3 Switzerland NON EU 131.4
## 4 4 Turks and Caicos Islands * NON EU 124.6
## 5 5 Israel NON EU 123.1
## 6 6 Barbados NON EU 121.8
## Monthly Income PPI
## 1 10,437 USD 103.8
## 2 5,433 USD 61.7
## 3 7,454 USD 89.1
## 4 2,013 USD 25.4
## 5 4,554 USD 58.1
## 6 1,613 USD 20.8
+-“-*+
mydata$MonthlyIncome <- gsub("[^0-9]", "", mydata$`Monthly Income`) #removing USD from the dataset
head(mydata)
## Rank Country EU MemberNon EU Member Cost Index
## 1 1 Bermuda * NON EU 157.9
## 2 2 Cayman Islands * NON EU 138.3
## 3 3 Switzerland NON EU 131.4
## 4 4 Turks and Caicos Islands * NON EU 124.6
## 5 5 Israel NON EU 123.1
## 6 6 Barbados NON EU 121.8
## Monthly Income PPI MonthlyIncome
## 1 10,437 USD 103.8 10437
## 2 5,433 USD 61.7 5433
## 3 7,454 USD 89.1 7454
## 4 2,013 USD 25.4 2013
## 5 4,554 USD 58.1 4554
## 6 1,613 USD 20.8 1613
mydata$my_column <- NULL
mydata$Monthly_Income <- NULL
mydata$`Monthly Income`<- NULL
head(mydata)
## Rank Country EU MemberNon EU Member Cost Index PPI
## 1 1 Bermuda * NON EU 157.9 103.8
## 2 2 Cayman Islands * NON EU 138.3 61.7
## 3 3 Switzerland NON EU 131.4 89.1
## 4 4 Turks and Caicos Islands * NON EU 124.6 25.4
## 5 5 Israel NON EU 123.1 58.1
## 6 6 Barbados NON EU 121.8 20.8
## MonthlyIncome
## 1 10437
## 2 5433
## 3 7454
## 4 2013
## 5 4554
## 6 1613
I removed the USD from Monthly Income column so there are only numeric values.
colnames(mydata) <- c("Rank","Country","EUMemberNonEUMember", "CostIndex" , "PurchasingPowerIndex", "MonthlyIncome")
head(mydata)
## Rank Country EUMemberNonEUMember CostIndex
## 1 1 Bermuda * NON EU 157.9
## 2 2 Cayman Islands * NON EU 138.3
## 3 3 Switzerland NON EU 131.4
## 4 4 Turks and Caicos Islands * NON EU 124.6
## 5 5 Israel NON EU 123.1
## 6 6 Barbados NON EU 121.8
## PurchasingPowerIndex MonthlyIncome
## 1 103.8 10437
## 2 61.7 5433
## 3 89.1 7454
## 4 25.4 2013
## 5 58.1 4554
## 6 20.8 1613
mydata$MonthlyIncome <- as.numeric(mydata$MonthlyIncome)
mydata$Rank <- as.numeric(mydata$Rank)
mydata$CostIndex <- as.numeric(mydata$CostIndex)
mydata$PurchasingPowerIndex <- as.numeric(mydata$PurchasingPowerIndex) #changing data to numeric values
head(mydata)
## Rank Country EUMemberNonEUMember CostIndex
## 1 1 Bermuda * NON EU 157.9
## 2 2 Cayman Islands * NON EU 138.3
## 3 3 Switzerland NON EU 131.4
## 4 4 Turks and Caicos Islands * NON EU 124.6
## 5 5 Israel NON EU 123.1
## 6 6 Barbados NON EU 121.8
## PurchasingPowerIndex MonthlyIncome
## 1 103.8 10437
## 2 61.7 5433
## 3 89.1 7454
## 4 25.4 2013
## 5 58.1 4554
## 6 20.8 1613
mean(mydata$CostIndex)
## [1] 59.69901
median(mydata$CostIndex)
## [1] 47.4
sd(mydata$CostIndex)
## [1] 32.24826
mean(mydata$MonthlyIncome)
## [1] 2034.129
median(mydata$MonthlyIncome)
## [1] 897
sd(mydata$MonthlyIncome)
## [1] 2283.377
mean(mydata$PurchasingPowerIndex)
## [1] 42.0198
median(mydata$PurchasingPowerIndex)
## [1] 29.8
sd(mydata$PurchasingPowerIndex)
## [1] 33.31965
mydata$EUMemberNonEUMember <- factor(mydata$EUMemberNonEUMember,
levels = c("EU", "NON EU"),
labels =c(1, 0))
1 = EU member country 0 = Non EU Member country
head(mydata)
## Rank Country EUMemberNonEUMember CostIndex
## 1 1 Bermuda * 0 157.9
## 2 2 Cayman Islands * 0 138.3
## 3 3 Switzerland 0 131.4
## 4 4 Turks and Caicos Islands * 0 124.6
## 5 5 Israel 0 123.1
## 6 6 Barbados 0 121.8
## PurchasingPowerIndex MonthlyIncome
## 1 103.8 10437
## 2 61.7 5433
## 3 89.1 7454
## 4 25.4 2013
## 5 58.1 4554
## 6 20.8 1613
summary(mydata)
## Rank Country EUMemberNonEUMember CostIndex
## Min. : 1 Length:101 1:16 Min. : 17.7
## 1st Qu.: 26 Class :character 0:85 1st Qu.: 34.8
## Median : 51 Mode :character Median : 47.4
## Mean : 51 Mean : 59.7
## 3rd Qu.: 76 3rd Qu.: 83.1
## Max. :101 Max. :157.9
## PurchasingPowerIndex MonthlyIncome
## Min. : 3.70 Min. : 100
## 1st Qu.: 14.20 1st Qu.: 320
## Median : 29.80 Median : 897
## Mean : 42.02 Mean : 2034
## 3rd Qu.: 65.20 3rd Qu.: 3748
## Max. :132.80 Max. :10437
summary(mydata[,c(-1,-2)]) #Excluding Country and rank
## EUMemberNonEUMember CostIndex PurchasingPowerIndex MonthlyIncome
## 1:16 Min. : 17.7 Min. : 3.70 Min. : 100
## 0:85 1st Qu.: 34.8 1st Qu.: 14.20 1st Qu.: 320
## Median : 47.4 Median : 29.80 Median : 897
## Mean : 59.7 Mean : 42.02 Mean : 2034
## 3rd Qu.: 83.1 3rd Qu.: 65.20 3rd Qu.: 3748
## Max. :157.9 Max. :132.80 Max. :10437
We can see that in this dataset there is a total of 101 countries with 16 EU Member Countries and 85 non EU member countries, with cost index at the minimum 17.7 and maximum 157.9 and a mean of 47.4, with purchasing power index there is a mean of 42.02, minimum of 3.7 and maximum of 132.8 and with monthly income there is a minimum of 100 and a maximum of 10437 - with all three we see that there is a wide range and difference between the minimum and maximum which indicates that the economic conditions of these countries are very diverse. Overall, this dataset provides a comparative analysis of countries, considering factors such as the cost of living, purchasing power, and income levels. This data is used to assess the economic conditions and disparities between EU Member and Non-EU Member regions or countries based on these key indicators.
library(ggplot2)
ggplot(mydata, aes(x = PurchasingPowerIndex))+
theme_bw()+
geom_histogram(binwidth = 10, colour = "blue", fill = "pink")+
ggtitle("PURCHASING POWER INDEX")+
ylab("Frequency")+
xlab("PPI")
With this histogram I showed that purchasing power index is most common
in for the value 0-20 and also shows that purchasing power index for the
value above 100 is quite rare.
library(car)
## Loading required package: carData
scatterplot(y = mydata$MonthlyIncome, x = mydata$PurchasingPowerIndex,
ylab = "MonthlyIncome",
xlab = "PurchasingPowerIndex",
boxplots = FALSE,
smooth = FALSE)
In this scatter plot I plotted the values of Monthly income on the Y axis and Purchasing Power Index on the X axis searching for a relationship. I assumed that there will be a positive correlation between Monthly income and PPI, with lower the income, lower the purchasing power. My assumption was correct as we can see with the regression line on the scatter plot, the slope is positive.
ggplot(mydata, aes(x = EUMemberNonEUMember, y = CostIndex, fill = EUMemberNonEUMember)) +
theme_bw()+
geom_boxplot() +
ylab("CostIndex")+
xlab("EUMemberNonEUMember")+
labs(fill = "EUMemberNonEUMember")
labs(title = "Box Plot of CostIndex by EUMemberNonEUMember")
## $title
## [1] "Box Plot of CostIndex by EUMemberNonEUMember"
##
## attr(,"class")
## [1] "labels"
1 - EU member Country 2 - Non EU member country
In this boxplot I decided to compare the Cost Indexes of EU Member Countries and Non EU member countries. As we can see EU Member countries have higher cost index and the median in the 3rd quartile, as for non EU member countries where the median is in the first quartile and in general the cost index range is lower. Although we can also see some outliers, minimum and maximum for the non EU countries.
mydata2 <- read.table("Body mass.csv",
sep = ";",
dec = ",",
header = TRUE)
library(pastecs)
round(stat.desc(mydata2[-1]), 2)
## Mass
## nbr.val 50.00
## nbr.null 0.00
## nbr.na 0.00
## min 49.70
## max 83.20
## range 33.50
## sum 3143.80
## median 62.80
## mean 62.88
## SE.mean 0.85
## CI.mean.0.95 1.71
## var 36.14
## std.dev 6.01
## coef.var 0.10
#install.packages("ggplot2")
library(ggplot2)
ggplot(mydata2, aes(x = Mass))+
theme_bw()+
geom_histogram(binwidth = 7, colour = "blue", fill = "yellow")+
ggtitle("Body Mass Distribution of 9th graders in 2021/22")+
ylab("Frequency")+
xlab("Body Mass in Kg")
t.test(mydata2$Mass,
mu = 59.5,
alternative = "two.sided")
##
## One Sample t-test
##
## data: mydata2$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
Using a two-tailed t-test, we investigated two hypotheses: H0 states that the average weight of 9th graders in 2018/19 is the same as in 2021/22, while Ha suggests they are different; our histogram suggests non-equality due to a peak between 62.5 and 67.5kg. Conducting the t-test at a 5% significance level, we reject the null hypothesis with a p-value of 0.0002 (0.02%), indicating that the 2018/19 mean weight is not equal to that of 2021/22; moreover, the 95% confidence interval (CI) for 2021/22’s mean weight, between 61.17 and 64.58, excludes the 2018/19 mean of 59.5.
#install.packages("readxl")
library(readxl)
mydata3 <- read_xlsx("~/Desktop/IMB/Bootcamp IMB/Apartments.xlsx")
Description:
mydata3$ParkingFactor <- factor(mydata3$Parking,
levels = c(0, 1),
labels = c("No", "Yes"))
mydata3$BalconyFactor <- factor(mydata3$Balcony,
levels = c(0, 1),
labels = c("No", "Yes"))
str(mydata3)
## tibble [85 × 7] (S3: tbl_df/tbl/data.frame)
## $ Age : num [1:85] 7 18 7 28 18 28 14 18 22 25 ...
## $ Distance : num [1:85] 28 1 28 29 18 12 20 6 7 2 ...
## $ Price : num [1:85] 1640 2800 1660 1850 1640 1770 1850 1970 2270 2570 ...
## $ Parking : num [1:85] 0 1 0 0 1 0 0 1 1 1 ...
## $ Balcony : num [1:85] 1 0 0 1 1 1 1 1 0 0 ...
## $ ParkingFactor: Factor w/ 2 levels "No","Yes": 1 2 1 1 2 1 1 2 2 2 ...
## $ BalconyFactor: Factor w/ 2 levels "No","Yes": 2 1 1 2 2 2 2 2 1 1 ...
t.test(mydata3$Price,
mu = 1900,
alternative = "two.sided")
##
## One Sample t-test
##
## data: mydata3$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
H0 : mu = 1900 Ha : mu is not = 1900
From that I can conclude that, at a 5% significance level (α = 5%), the average apartment price is statistically different from 1900, leading to the rejection of H0. The 95% confidence interval for apartment prices falls between 1937.443 and 2100.440, indicating with 95% confidence that the true population mean lies within this range, excluding the value of 1900, which reinforces the rejection of the null hypothesis at a 5% significance level. #### Estimate the simple regression function: Price = f(Age). Save results in object fit1 and explain the estimate of regression coefficient, coefficient of correlation and coefficient of determination.
fit1 <- lm(Price ~ Age,
data = mydata3)
summary(fit1)
##
## Call:
## lm(formula = Price ~ Age, data = mydata3)
##
## 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
sqrt(summary(fit1)$r.squared) # Coefficient of correlation (in absolute terms)
## [1] 0.230255
The regression equation is formulated as Price = 2185.455 - 8.975 x Age.
The intercept coefficient signifies that, on average, when Age is 0 and all other factors remain constant, the price is 2185.455. The coefficient for Age indicates that for every one-unit increase in Age, the price, on average, decreases by 8.975, holding all other variables constant. Both coefficients have p-values below 0.05, leading to the rejection of the null hypothesis for each. The null hypothesis tests whether the coefficient is equal to 0, and at a 5% significance level (or even lower for the intercept), we can confidently assert that the coefficients are not equal to 0.
The multiple R-squared statistic, or coefficient of determination, reveals that 5.302% of the price variability is accounted for by the independent variable Age.
The square root of R-squared, the coefficient of correlation, denotes the absolute correlation between the observed and predicted variables, which, in this case, amounts to 0.23.
#install.packages("car")
library(car)
scatterplotMatrix(mydata3[ , 1:3],
smooth = FALSE)
Multicollinearity concerns arise when explanatory variables exhibit
significant correlations with each other. In this scenario, we can
assess the presence of multicollinearity by examining the regression
line in the scatter plot between the explanatory variables, such as Age
and Distance. Upon closer inspection of the plot, it becomes evident
that the slope of the line closely approximates zero, indicating a lack
of substantial correlation between these two variables. Consequently, we
can confidently conclude that there is no issue with multicollinearity
in this context.
fit2 <- lm(Price ~ Age + Distance,
data = mydata3)
summary(fit2)
##
## Call:
## lm(formula = Price ~ Age + Distance, data = mydata3)
##
## 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
mean(vif(fit2))
## [1] 1.001845
The VIF (Variance Inflation Factor) statistic is utilized to examine the presence of multicollinearity among the explanatory variables. The guideline for assessing VIF is that it should remain below 5 for all explanatory variables, with the mean VIF approaching one. When a variable exceeds a VIF of 5, it raises concerns about multicollinearity and warrants further investigation or removal. In our specific scenario, both explanatory variables exhibit VIF values below 5, and the mean VIF is closely aligned with one, affirming the absence of multicollinearity as an issue.
mydata3$StdResid <- round(rstandard(fit2), 3)
head(mydata3[order(-mydata3$StdResid), ])
## # A tibble: 6 × 8
## Age Distance Price Parking Balcony ParkingFactor BalconyFactor StdResid
## <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <fct> <dbl>
## 1 5 45 2180 1 1 Yes Yes 2.58
## 2 2 11 2790 1 0 Yes No 2.05
## 3 18 1 2800 1 0 Yes No 1.78
## 4 18 1 2800 1 1 Yes Yes 1.78
## 5 8 2 2820 1 0 Yes No 1.66
## 6 10 1 2810 0 0 No No 1.60
tail(mydata3[order(-mydata3$StdResid), ])
## # A tibble: 6 × 8
## Age Distance Price Parking Balcony ParkingFactor BalconyFactor StdResid
## <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <fct> <dbl>
## 1 24 5 1830 1 0 Yes No -1.19
## 2 14 16 1660 0 1 No Yes -1.26
## 3 13 8 1800 0 0 No No -1.38
## 4 12 14 1650 0 1 No Yes -1.50
## 5 12 14 1650 0 0 No No -1.50
## 6 7 2 1760 0 1 No Yes -2.15
mydata3$CooksD <- round(cooks.distance(fit2), 3)
head(mydata3[order(-mydata3$CooksD), ])
## # A tibble: 6 × 9
## Age Distance Price Parking Balcony ParkingFactor BalconyFactor StdResid
## <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <fct> <dbl>
## 1 5 45 2180 1 1 Yes Yes 2.58
## 2 43 37 1740 0 0 No No 1.44
## 3 2 11 2790 1 0 Yes No 2.05
## 4 7 2 1760 0 1 No Yes -2.15
## 5 37 3 2540 1 1 Yes Yes 1.58
## 6 40 2 2400 0 1 No Yes 1.09
## # ℹ 1 more variable: CooksD <dbl>
hist(mydata3$CooksD,
xlab = "Distance",
ylab = "Frequency",
main = "Cooks Distances")
hist(mydata3$StdResid,
xlab = "Standardized Residuals",
ylab = "Frequency",
main = "Standardized Residuals")
Upon examining the standardized residuals, it’s evident that none exceed the threshold of 3 or fall below -3, which would classify them as outliers, warranting their removal. However, a notable observation stands out when assessing Cook’s distance, displaying a significantly higher value compared to all others. This signifies that this particular observation exerts a more pronounced influence on the regression than the rest. Additionally, considering that the standardized residual of this observation closely approaches 3 (specifically, 2.577), I have chosen to exclude it from my dataset. This removal will be executed at a later stage when re-estimating fit 2 to preempt any potential complications in the subsequent two exercises. In the forthcoming exercises, I will utilize the regression model with the “outlier” retained. #### Check for potential heteroskedasticity with scatterplot between standarized residuals and standrdized fitted values. Explain the findings.
mydata3$StdFittedValues <- scale(fit2$fitted.values)
scatterplot(y = mydata3$StdResid, x = mydata3$StdFittedValues,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
Heteroskedasticity arises when the variance is not consistent across the
data points. In the presented scatterplot, it’s apparent that the
variance of standardized residuals remains uniform throughout.
Consequently, we can conclude that there is no heteroskedasticity, and
instead, we observe homoskedasticity, which is not a cause for concern.
#### Are standardized residuals ditributed normally? Show the graph and
formally test it. Explain the findings.
hist(mydata3$StdResid,
prob = TRUE,
xlab = "Standardized residuals",
ylab = "Density",
main = "Standardized Residuals")
curve(dnorm(x, mean = mean(mydata3$StdResid), sd = sd(mydata3$StdResid)),
add = TRUE,
lwd = 2,
col = "red")
shapiro.test(mydata3$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata3$StdResid
## W = 0.95303, p-value = 0.003645
The histogram analysis already suggests that the standardized residuals do not follow a normal distribution, as their shape does not align with that of a typical normal distribution curve. When we subject them to a formal normality test using the Shapiro-Wilk test, where the null hypothesis (H0) posits a normal distribution and the alternative hypothesis (Ha) suggests otherwise, we can confidently reject the null hypothesis at a 5% significance level. This rejection implies that the distribution is indeed non-normal.
mydata3 <- mydata3[!(mydata3$CooksD == 0.320), ]
head(mydata3[order(-mydata3$CooksD), ])
## # A tibble: 6 × 10
## Age Distance Price Parking Balcony ParkingFactor BalconyFactor StdResid
## <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <fct> <dbl>
## 1 43 37 1740 0 0 No No 1.44
## 2 2 11 2790 1 0 Yes No 2.05
## 3 7 2 1760 0 1 No Yes -2.15
## 4 37 3 2540 1 1 Yes Yes 1.58
## 5 40 2 2400 0 1 No Yes 1.09
## 6 8 2 2820 1 0 Yes No 1.66
## # ℹ 2 more variables: CooksD <dbl>, StdFittedValues <dbl[,1]>
fit2 <- lm(Price ~ Age + Distance,
data = mydata3)
summary(fit2)
##
## Call:
## lm(formula = Price ~ Age + Distance, data = mydata3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -604.92 -229.63 -56.49 192.97 599.35
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2456.076 73.931 33.221 < 2e-16 ***
## Age -6.464 3.159 -2.046 0.044 *
## Distance -22.955 2.786 -8.240 2.52e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 276.1 on 81 degrees of freedom
## Multiple R-squared: 0.4838, Adjusted R-squared: 0.4711
## F-statistic: 37.96 on 2 and 81 DF, p-value: 2.339e-12
sqrt(summary(fit2)$r.squared)
## [1] 0.6955609
I have successfully removed the observation that was deemed necessary to exclude, specifically the one with a Cook’s distance of 0.320 and a standardized residual of 2.577. Subsequently, I conducted a verification to ensure the observation’s proper removal, which was confirmed. I then proceeded to re-estimate fit2 to “refresh” the regression model without the excluded observation. The regression equation is formulated as Price = 2456.076 - 6.464 x Age - 22.955 x Distance. The intercept coefficient signifies that, on average, when both Age and Distance are zero, and all other factors remain constant, the price amounts to 2456.076. The regression coefficient for Age indicates that for each one-unit increase in Age, the price, on average, decreases by 6.464, holding all other variables constant. Similarly, the regression coefficient for Distance suggests that for each one-unit increase in Distance, the price, on average, decreases by 22.955, assuming other variables remain constant. Notably, all three coefficients exhibit p-values below 0.05, leading us to reject the null hypothesis for each. The null hypothesis tests whether the coefficient is equal to 0, and at a 5% significance level (or even lower for the intercept and Distance coefficient), we can confidently assert that the coefficients are not equal to 0. The multiple R-squared statistic, or coefficient of determination, reveals that 48.38% of the price variability is accounted for by the independent variables Age and Distance. The square root of R-squared, the coefficient of correlation, denotes the absolute correlation between the observed and predicted variables, which, in this case, equals 0.6955609.
fit3 <- lm(Price ~ Age + Distance + ParkingFactor + BalconyFactor,
data = mydata3)
anova(fit2, fit3)
## Analysis of Variance Table
##
## Model 1: Price ~ Age + Distance
## Model 2: Price ~ Age + Distance + ParkingFactor + BalconyFactor
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 81 6176767
## 2 79 5654480 2 522287 3.6485 0.03051 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
By performing an analysis of variance (ANOVA), we can determine, at a 5% significance level, that the fit3 regression provides a superior fit to the data compared to fit2. This conclusion arises from the observation that when the p-value falls below 0.05, ANOVA favors the more complex model (fit3), whereas when the p-value exceeds 0.05, ANOVA indicates a preference for the simpler model (fit2).
summary(fit3)
##
## Call:
## lm(formula = Price ~ Age + Distance + ParkingFactor + BalconyFactor,
## data = mydata3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -473.21 -192.37 -28.89 204.17 558.77
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2329.724 93.066 25.033 < 2e-16 ***
## Age -5.821 3.074 -1.894 0.06190 .
## Distance -20.279 2.886 -7.026 6.66e-10 ***
## ParkingFactorYes 167.531 62.864 2.665 0.00933 **
## BalconyFactorYes -15.207 59.201 -0.257 0.79795
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 267.5 on 79 degrees of freedom
## Multiple R-squared: 0.5275, Adjusted R-squared: 0.5035
## F-statistic: 22.04 on 4 and 79 DF, p-value: 3.018e-12
The regression coefficient associated with Parking signifies that if an apartment includes a parking spot, the average price will increase by 167.531, assuming all other factors remain constant. Notably, this coefficient also exhibits a p-value below 0.01, allowing us to assert, at a 1% significance level, that the coefficient significantly differs from 0. On the other hand, the coefficient for Balcony carries a p-value of 0.79795, which prevents us from making a similar conclusion regarding its deviation from 0. In the context of the F-statistic test, the null hypothesis posits that the population coefficient of determination equals 0, while the alternative hypothesis suggests that the population coefficient of determination is greater than 0.
mydata3$FittedValues <- fitted.values(fit3)
mydata3$Residuals <- residuals(fit3)
round(mydata3[2, 12], 2)
## # A tibble: 1 × 1
## Residuals
## <dbl>
## 1 428.
The residual associated with apartment ID2 amounts to 427.8, indicating that the disparity between the actual price and the price predicted by the regression model is 427.8. In other words, the actual price exceeds the estimated value by the regression by 427.8.