#TASK 1 ##ANŽE AMBROŽIČ
data()
data(package = .packages(all.available = TRUE))
#install.packages("survival")
library("survival")
mydata <- force(transplant)
head(mydata)
## age sex abo year futime event
## 1 47 m B 1994 1197 death
## 2 55 m A 1991 28 ltx
## 3 52 m B 1996 85 ltx
## 4 40 f O 1995 231 ltx
## 5 70 m O 1996 1271 censored
## 6 66 f O 1996 58 ltx
There are 6 variables used in the data set Transplant. Age: age when patients have been added to the waiting list Sex: the gender of patient Abo: blood type of the patient Year: Year in which patient entered the waiting list Futime:time from the entry to the waiting list to final disposition Event: Final disposition of patient
mydata$BirthYear <- mydata$year - mydata$age #Creating new Column called BirthYear
mydataCleaned <-na.omit(mydata) #Creating new data without obsitcles with NA data
names(mydataCleaned)[names(mydataCleaned) == "abo"] <- "BloodType" #Renaming Column abo to BloodType
mydataCleaned$event <- NULL #Deleting event column
head(mydataCleaned)
## age sex BloodType year futime BirthYear
## 1 47 m B 1994 1197 1947
## 2 55 m A 1991 28 1936
## 3 52 m B 1996 85 1944
## 4 40 f O 1995 231 1955
## 5 70 m O 1996 1271 1926
## 6 66 f O 1996 58 1930
summary(mydataCleaned[ ,c(-2, -3)])
## age year futime BirthYear
## Min. :17.00 Min. :1990 Min. : 0.0 Min. :1919
## 1st Qu.:44.00 1st Qu.:1993 1st Qu.: 50.0 1st Qu.:1937
## Median :52.00 Median :1996 Median : 127.0 Median :1944
## Mean :50.52 Mean :1995 Mean : 214.3 Mean :1945
## 3rd Qu.:58.00 3rd Qu.:1997 3rd Qu.: 277.0 3rd Qu.:1952
## Max. :72.00 Max. :1999 Max. :2055.0 Max. :1979
Mean: AGE (50.52),The average age of patients that are included in the data set is 50.52 years of age. MEDIAN: YEAR (1990), The median year in which patients entered the waiting list is 1990. That means that half of population have entered the waiting list before 1990 and the other half of the population have entered the waiting lsit after the year 1990. MIN : BIRTHYEAR (1919) The minimum Year of birth of the patients is year 1919. That means that the oldest patient has been born in the year 1919 and others have been born after.
hist(mydataCleaned$age,
main = "Histogram of ages at addiition to the waiting list",
xlab = "Age",
ylab = "Frequency",
breaks = seq(from=0, to=80, by=2))
The findings of the histogram show frequency of ages of the patients.
The highest column shows the age with the highest frequency, also known
as mode.
boxplot(mydataCleaned$age,
main="Boxplot of age",
xlab="Variabile",
ylab="Age",
col="orange",
horizontal=TRUE,
notch=FALSE,
outline=FALSE)
The box plot of age shows the minimum and maximum of ages. The black
line is median. In the summary we find the median age at 52.In the
orange box we can see the visual presentation from 25th till 75
percentile. Outside the orange box are first and fourth quantile of
values.
#TASK 2
mydataX <- read.table("./Body mass.csv", header = TRUE, sep = ";", dec = ",")
head(mydataX)
## 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
DESCRIPTIVE STATISTICS
#install.packages("psych")
library(psych)
describe(mydataX[ , c(-1)])
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 50 62.88 6.01 62.8 62.56 3.34 49.7 83.2 33.5 0.85 2.11 0.85
Histogram showing distribution of body mass
hist(mydataX$Mass,
breaks = 4,
col = "green",
border = "grey",
main = "Weight of nine graders",
xlab = "Body mass",
ylab = "Frequency")
T TEST (mean)
H0: Mu = 59.5 H1: Mu =/ 59.5
mean(mydataX$Mass)
## [1] 62.876
result <- t.test(mydataX$Mass, mu = 59.5)
cat("P-value:", result$p.value,"\n")
## P-value: 0.0002339924
Effectsize
library("effectsize")
##
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
##
## phi
cohens_d(mydataX$Mass, mu= 59.5)
## Cohen's d | 95% CI
## ------------------------
## 0.56 | [0.26, 0.86]
##
## - Deviation from a difference of 59.5.
interpret_cohens_d(0.56, "sawilowsky2009")
## [1] "medium"
## (Rules: sawilowsky2009)
With the usage of T-test we have tested the mean of the average weight of the nine graders. The null hypothesis is “the average weight of the nine graders is 59.5kg. The alternative hypothesis is”weight of the nine graders is not 59.5 kg”. The result of the T-test shows that the P-value is 0.0002339924. Since the P-value is smaller than 0.05 we reject the null hypothesis and accept the alternative hypothesis. The critical value is alpha= 5%. With 95% certainty we can say that the average weight is not 59.5kg.
The average weight is 62.876. We found out that using the function mean.
We used function cohens_d to find the effect size used to to indicate the standardized difference between two means. To interpret the difference we have used function interpret_cohens_d, with sawilowsky2009 rules and the results have shown that the effect is medium.
#TASK 3
#install.packages("readxl")
library(readxl)
mydata1 <- read_xlsx("./Apartments.xlsx")
Description:
mydata1$ParkingFactor <- factor(mydata1$Parking,
levels = c(1, 0),
labels = c("YES", "NO"))
mydata1$BalconyFactor <- factor(mydata1$Balcony,
levels = c(1, 0),
labels = c("YES", "NO"))
t.test(mydata1$Price,
mu = 1900,
alternative = "two.sided")
##
## One Sample t-test
##
## data: mydata1$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
Since the P-value of T-test is smaller than =0.05 (P-value is 0.004731), we can reject the null hypothesis and accept the altenrative hypothesis that says the average value is not 1900.
#install.packages("ggplot2")
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
lm(Price ~ Age, data = mydata1)
##
## Call:
## lm(formula = Price ~ Age, data = mydata1)
##
## Coefficients:
## (Intercept) Age
## 2185.455 -8.975
fit1 <- lm(Price ~ Age, data = mydata1)
summary(fit1)
##
## Call:
## lm(formula = Price ~ Age, data = mydata1)
##
## 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
r <- sqrt(0.05302)
The estimate of regression coefficient for “Age” is -8.975.That means that if any of apartments gets older for one year, on average the price will decrease for 8.975€ The estimate coefficient of determination is 0.05302.It means that only 5.3% of variablity in price is expalined by linereal relationships with Age. The estimate coefficient of correlation is 0.230. Positive correlation indicates that as Age increases the price will decrease.
#install.packages(car)
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
scatterplotMatrix(mydata1[ , c(-4, -5, -6, -7)],
smooth = FALSE)
In the scateerplot we don’t find any signs of multicolinearity.
fit2 <- lm(Price ~ Age + Distance,
data = mydata1)
summary(fit2)
##
## Call:
## lm(formula = Price ~ Age + Distance, data = mydata1)
##
## 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
Since both of vif results are under 5 there is no sign of multicolinearity. Also both values are close to 1 (VIF 1, means that there is no multicolinearity).
mydata1$StdResid <- round(rstandard(fit2), 3)
mydata1$CooksD <- round(cooks.distance(fit2), 3)
By analysing cooks distances we have found only one problemativ obstacle with larger cook distance than the rest of the population. We will remove the row 38.
mydata1$StdFitted <- scale(fit2$fitted.values)
library(car)
scatterplot(y = mydata1$StdResid, x = mydata1$StdFitted,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
Thereis no sign of heteroskedasticity. #### Are standardized residuals
ditributed normally? Show the graph and formally test it. Explain the
findings.
hist(mydata1$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main = "Histogram of standardized residuals")
shapiro.test(mydata1$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata1$StdResid
## W = 0.95303, p-value = 0.003645
We prepared the histogram of standardized residuals and also shapiro test. P-value of shapiro test is 0.003645. That means that we have to reject the H0:the data is normally distributed. We accept H1: the data is not normally distributed. We can also see the distribution of data in the histogram, that clearly indicates there is not a normal distribution.
mydata2 <- mydata1 [-38, ]
fit2 <- lm(Price ~ Age + Distance,
data = mydata2)
summary(fit2)
##
## Call:
## lm(formula = Price ~ Age + Distance, data = mydata2)
##
## 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
Estimate for Age: -6.464 indicates that for every additional year the price falls on average for 6.464€
Estimate of Distance: -22.955 indicates that for every additional kilometer of distance from center the price falls on average for 22.995€
P-values: All P-values are lower than 0.05 (Alpha is 5%), so we reject all null hypothesis and accept alternatives.
Residual standard error of 276.1 means that price values in data set deviate from predicted values by 276.1 units.
R-squared: value is 0.4838 means that 48.38% of the variability in “Price” can be explained ba Age and distance.
fit3 <- lm(Price ~ Age + Distance + ParkingFactor + BalconyFactor, data=mydata2)
summary(fit3)
##
## Call:
## lm(formula = Price ~ Age + Distance + ParkingFactor + BalconyFactor,
## data = mydata2)
##
## 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) 2482.048 79.263 31.314 < 2e-16 ***
## Age -5.821 3.074 -1.894 0.06190 .
## Distance -20.279 2.886 -7.026 6.66e-10 ***
## ParkingFactorNO -167.531 62.864 -2.665 0.00933 **
## BalconyFactorNO 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
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
Null hypothesis is that there is no difference between fit 2 and fit 3. Alternative hypothesis is that there is a difference between fit 2 and fit 3 model. P value is 0.03051. That means that we reject the null hypothesis and accept alternative. The ANOVA results indicates that FIT 3 is a better fit.
summary(fit3)
##
## Call:
## lm(formula = Price ~ Age + Distance + ParkingFactor + BalconyFactor,
## data = mydata2)
##
## 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) 2482.048 79.263 31.314 < 2e-16 ***
## Age -5.821 3.074 -1.894 0.06190 .
## Distance -20.279 2.886 -7.026 6.66e-10 ***
## ParkingFactorNO -167.531 62.864 -2.665 0.00933 **
## BalconyFactorNO 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
H0: There is no relationship between b1, b2, b3 and b4. H1: b1,b2,b3,b4 != 0. At least one of the variables is not worth zero.
P value for the test is 3.018e-12. That means that we reject the null hypothesis and accept the alternative hypothesis.
mydata2$residuals <- residuals(fit3)
mydata2$fittedValues <- fitted.values(fit3)
head(mydata2[2, c(11,12)])
## # A tibble: 1 × 2
## residuals fittedValues
## <dbl> <dbl>
## 1 428. 2372.