# vidko = E6062
# data import
dataset <- read.csv("C:\\Users\\Paulina\\Desktop\\DA\\lab3_set1.csv");
mydata <- dataset[c("V2")];
(2 points)
x <- mydata$V2;
mean <- mean(mydata$V2);
sd <- sd(mydata$V2);
alpha <- 0.05;
intervalsH <- hist(x, main = "Histogram of the data sample: V2", prob = TRUE);
curve(dnorm(x, mean = mean, sd = sd), col = "red", lwd = 2, add = TRUE);
standarize <- (x - mean) / sd;
library(nortest)
ADtest <- ad.test(standarize);
SWtest <- shapiro.test(x);
print(ADtest);
##
## Anderson-Darling normality test
##
## data: standarize
## A = 0.31087, p-value = 0.553
print(SWtest);
##
## Shapiro-Wilk normality test
##
## data: x
## W = 0.99876, p-value = 0.7312
breaks_cdf <- pnorm(intervalsH$breaks, mean = mean(mydata$V2), sd = sd(mydata$V2)) ;
null.probs <- zoo::rollapply(breaks_cdf, 2, function(x) x[2] - x[1]);
CStest <- chisq.test(intervalsH$counts, p = null.probs, rescale.p = TRUE, simulate.p.value = TRUE);
print(CStest);
##
## Chi-squared test for given probabilities with simulated p-value (based
## on 2000 replicates)
##
## data: intervalsH$counts
## X-squared = 9.187, df = NA, p-value = 0.6062
print("Quantile of Chi-squared distribution:");
## [1] "Quantile of Chi-squared distribution:"
qchisq(alpha, length(intervalsH$breaks) - 2 - 1, lower.tail = FALSE);
## [1] 18.30704
Formulate the hypothesis for
Anderson-Darling Test / Kolmogorov-Smirnov Test.
- H0: The data
are normally distributed
- Ha: The data are not normally
distributed
- Conclusion from testing: The calculated p-value -
0.5529679 is greater than the significance level - 0.05, meaning we fail
to reject H0
Formulate the
hypothesis for Shapiro-Wilk Test.
- H0: The data are normally
distributed
- Ha: The data are not normally distributed
-
Conclusion from testing: The calculated p-value - 0.7311716 is greater
than the significance level - 0.05, meaning we fail to reject H0
Formulate the the hypothesis for
Pearson’s Chi-squared Test.
- H0: The data are normally
distributed
- Ha: The data are not normally distributed
-
How many bins you got? 12
- What is the meaning of pnorm()? The
function returns the cumulative probability of the standard normal
distribution at Z score
- Conclusion from testing: the null
hypothesis cannot be rejected because the p-value 0.5937 is more than
the significance lever 0.05, this means that the data is normally
distributed
- Find quantile of Chi-squared distribution qchisq()
and make conclusion from the hypothesis comparing test statistic and
quantile. The value of the quantile is 18.30704 and the test statistic
is 9.187. Since the test statistic is lower than the quantile so we
cannot reject the null hypothesis
(1 point)
Assume that data could be described using Student’s t distribution.
- apply one any goodness-of-fit test to to test whether sample X
can be described using Student’s t distribution.
- repeat the same
steps following task 1.1.
- - p.s. add curve of Student’s t
distribution dt() instead of normal distribution. - Answer the questions
below.
library(goftest)
params<-QRM::fit.st(mydata$V2);
dof<-params$par.ests[["nu"]];
ad.test(x = standarize,null = "pt", df = dof)
##
## Anderson-Darling test of goodness-of-fit
## Null hypothesis: Student's t distribution
## with parameter df = 242.299227990362
## Parameters assumed to be fixed
##
## data: standarize
## An = 0.31066, p-value = 0.9299
ad.test(x = standarize,null = "pt", df = 3)
##
## Anderson-Darling test of goodness-of-fit
## Null hypothesis: Student's t distribution
## with parameter df = 3
## Parameters assumed to be fixed
##
## data: standarize
## An = 10.008, p-value = 7.289e-06
hist(standarize, prob = TRUE, col = "lightblue", main = "Fitted Student's t Distribution");
curve(dt(x, df = dof), col = "red", lwd = 2, add = TRUE);
curve(dt(x, df = 3), col = "green", lwd = 2, add = TRUE);
What is the value of degrees of freedom ?
242.2992 Formulate the hypothesis of
selected test
- H0: The data follows Student’s t distribution with
242.2992 degrees of freedom
- Ha: The data does not follow
Student’s t distribution with 242.2992 degrees of freedom
-
Conclusion from testing: the null hypothesis cannot be rejected because
the p-value 0.9299 is more than significance level 0.05
Now choose degrees of freedom df=3. What
results from the hypothesis you got? the null hypothesis(when df=3)
can’t be rejected because the p-value 7.289e-06 is less than
significance level 0.05.
…
We assume that X - our population, and we create other smaller samples (e.g. Y, Z) of size 100 observations randomly selected from X
set.seed(404);
Y <- mydata$V2[sample(length(mydata$V2), 0.1*length(mydata$V2))];
set.seed(202);
Z <- mydata$V2[sample(length(mydata$V2), 0.1*length(mydata$V2))];
print(Y);
## [1] 27.469728 13.279629 12.221634 15.609902 22.607129 19.711304 7.154906
## [8] 18.194363 23.599164 14.439348 22.969842 22.343794 30.079586 30.691978
## [15] 3.812775 5.060942 18.902193 18.061315 7.391197 11.848825 21.455071
## [22] 28.260516 20.798083 3.820431 1.797357 14.091178 11.785067 38.306194
## [29] 11.815126 20.402375 37.002645 13.623142 13.449469 28.433849 13.190188
## [36] 19.242854 21.239942 21.839173 17.131109 25.350885 23.897260 18.297287
## [43] 22.193338 21.569297 20.084353 20.331743 8.902057 16.838442 18.930742
## [50] 20.431577 27.654463 16.703486 11.892043 15.914143 27.408696 19.385538
## [57] 25.350802 27.869690 26.836594 10.984688 19.731995 18.430904 21.257681
## [64] 20.842572 0.493994 22.968218 22.550141 23.662115 17.736496 18.699249
## [71] 1.499406 15.457212 11.251742 9.282189 28.173318 22.464303 6.446852
## [78] 23.438526 9.093165 17.938111 19.348751 23.520909 13.675207 10.118482
## [85] 13.414450 34.579152 14.336134 16.939292 23.714959 32.782499 16.425689
## [92] 6.696068 24.250716 15.239367 32.798243 21.688672 16.363297 31.995762
## [99] 8.550150 27.570519
print(Z);
## [1] 12.334832 21.249381 26.140827 16.683405 36.186597 42.498106 23.140500
## [8] 16.703486 19.366238 23.599164 13.883773 8.165561 22.882946 22.066477
## [15] 17.992462 4.915701 15.732779 18.872870 33.246038 22.969842 30.890730
## [22] 3.731129 10.118482 6.717366 9.492470 12.231319 20.026889 21.829296
## [29] 27.373918 16.628807 14.047229 24.928880 17.289237 16.784860 3.989177
## [36] 22.363843 19.223133 18.716459 20.509517 14.138891 35.873238 12.589928
## [43] 24.373825 31.056532 22.239455 9.703760 3.181718 17.105908 23.803240
## [50] 18.600128 27.593635 20.866687 9.943721 26.459073 40.348107 25.576864
## [57] 17.354427 39.849205 15.296590 10.873669 8.859042 31.267254 16.101996
## [64] 14.086769 29.113847 30.384401 21.816322 15.175142 14.128953 7.947577
## [71] 12.527985 22.464303 24.111434 18.902193 25.990450 31.855101 19.134811
## [78] 17.994987 34.257631 13.492864 34.924463 22.968218 16.939292 14.461313
## [85] 11.454232 17.947802 11.695717 23.652577 24.150441 8.023239 24.940752
## [92] 23.664290 22.747354 14.456059 21.351987 24.021214 11.670721 17.469968
## [99] 27.326018 8.902057
(1 point)
mu0 <- mean(x); # hypothesized value
print(mu0)
## [1] 20.06108
xbar <- mean(Y);
s <- sd(Y);
n <- length(Y);
tMan <- (xbar - mu0)/(s/sqrt(n));
tFun <- t.test(Y, mu = mu0, alternative = "two.sided");
print("Two tailed test manual and function implementation")
## [1] "Two tailed test manual and function implementation"
print(tMan);
## [1] -1.780239
print(tFun);
##
## One Sample t-test
##
## data: Y
## t = -1.7802, df = 99, p-value = 0.0781
## alternative hypothesis: true mean is not equal to 20.06108
## 95 percent confidence interval:
## 17.08542 20.22232
## sample estimates:
## mean of x
## 18.65387
critVal <- qt(1 - alpha / 2, df = n -1);
print("Critical values for two-tailed test")
## [1] "Critical values for two-tailed test"
c(-critVal, critVal);
## [1] -1.984217 1.984217
critVal1tail <- qt(1 - alpha, df = n - 1);
print("Critical value for one-tailed test")
## [1] "Critical value for one-tailed test"
print(critVal1tail);
## [1] 1.660391
tFun1tail <- t.test(Y, mu = mu0, alternative = "greater");
print("One tailed test(upper) manual and function implementation");
## [1] "One tailed test(upper) manual and function implementation"
print(tMan);
## [1] -1.780239
print(tFun1tail);
##
## One Sample t-test
##
## data: Y
## t = -1.7802, df = 99, p-value = 0.9609
## alternative hypothesis: true mean is greater than 20.06108
## 95 percent confidence interval:
## 17.34139 Inf
## sample estimates:
## mean of x
## 18.65387
Formulate hypothesis for two.tailed test.
- H0: The mean is equal to hypothesized mean
- Ha: The mean
is not equal to hypothesized mean
- Conclusion from your procedure
with explanation why: the test statistic lies between critical values
-1.984217 and 1.984217 meaning that we do not reject null hypothesis
- Conclusion from built-in function with explanation why: we do
not reject null hypothesis because the p-value 0.0781 is greater than
the significance level 0.05
Formulate hypothesis for one one.tailed test.
- H0: The mean is
lower than or equal to the hypothesized mean
- Ha: The mean is
greater than the hypothesized mean.
- Conclusion from your
procedure with explanation why: we fail to reject null hypothesis
because the test value -1.780239 is lower than the critical value
1.660391
- Conclusion from built-in function with explanation why:
we do not reject null hypothesis because the p-value 0.9609 is greater
than the significance level 0.05
(1 point)
xbarZ <- mean(Z);
print("Mean of Y:")
## [1] "Mean of Y:"
print(xbar);
## [1] 18.65387
print("Mean of Z:")
## [1] "Mean of Z:"
print(xbarZ);
## [1] 19.74733
sZ <- sd(Z)
print(s);
## [1] 7.904631
print(sZ);
## [1] 8.389046
twoTailYZ <- t.test(Y, Z, alternative = "two.sided");
print(twoTailYZ);
##
## Welch Two Sample t-test
##
## data: Y and Z
## t = -0.94865, df = 197.3, p-value = 0.344
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -3.366549 1.179626
## sample estimates:
## mean of x mean of y
## 18.65387 19.74733
oneTailYZ <- t.test(Y, Z, alternative = "less");
print(oneTailYZ);
##
## Welch Two Sample t-test
##
## data: Y and Z
## t = -0.94865, df = 197.3, p-value = 0.172
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf 0.8114165
## sample estimates:
## mean of x mean of y
## 18.65387 19.74733
Means of \(Y\) and \(Z\): 18.65387 and 19.74733
Standard
deviations of \(Y\) and \(Z\): 7.904631 and 8.389046
Formulate hypothesis for two.tailed test.
- H0: Means are equal
- Ha: Means are not equal
-
Conclusion: we do not reject the null hypothesis becaues p-value 0.344
is more than significance value 0.05
Formulate hypothesis for one one.tailed test.
- H0: The mean of Y is less than or equal to the mean of Z
- Ha:
The mean of Y is more than the mean of Z
- Conclusion: we do not
reject the null hypothesis becaues p-value 0.172 is more than
significance value 0.05
You are given a data sample about the posts published in the
social network. Sample includes the main variable “likes” and two
additional variables: post’s topic, video indicator (duration or no
video) and comment to some question.
The goal – to determine the existence of a statistically
significant difference among the means of “likes” conditioned by two
factors “topic” or “video”. Method - ANOVA
analysis. All steps and tasks are given below.
You can follow the theory slides of the examples here: https://www.datanovia.com/en/lessons/anova-in-r/ or http://www.sthda.com/english/wiki/one-way-anova-test-in-r#what-is-one-way-anova-test or http://www.sthda.com/english/wiki/kruskal-wallis-test-in-r#multiple-pairwise-comparison-between-groups
(0.5 point)
The data are sampled from the file “lab3_set2.csv” found in the
Moodle. For this purpose ‘sample’ function is used to randomly select
90% data from the imported data set. Use set.seed() as parameter to
control random number generation every time you generate them.
Verify data types with function str(). ‘topic’and ’video’ should
be factors, ‘likes’- numeric (integer).
Answer the questions below.
duom_raw <- read.csv2("C:\\Users\\Paulina\\Desktop\\DA\\lab3_set2.csv");
set.seed(6062);
mydata <- duom_raw[sample(nrow(duom_raw),0.9*nrow(duom_raw)),];
mydata$video <- as.factor(mydata$video);
mydata$topic <- as.factor(mydata$topic);
mydata$comments <- as.factor(mydata$comments);
str(mydata)
## 'data.frame': 900 obs. of 4 variables:
## $ likes : int 118 115 124 141 208 126 174 180 126 179 ...
## $ video : Factor w/ 4 levels "Long","Medium",..: 3 3 3 3 4 3 1 2 3 1 ...
## $ topic : Factor w/ 3 levels "P1","P2","P3": 1 1 1 3 1 1 1 2 2 1 ...
## $ comments: Factor w/ 3 levels "Agree","Disagree",..: 3 3 3 3 1 3 1 1 3 2 ...
- How many factors you have in the sample?
2(3 if including comments)
- How many levels each factor has?
Video - 4 levels, topic - 3 levels, comments - 3 levels
- What is
the dependent (response) variable? Likes
(0.5 point)
meanLikes <- mean(mydata$likes);
sdLikes <- sd(mydata$likes);
noVideo = mydata[mydata$video == "No",];
meanNoVideo = mean(noVideo$likes);
sdNoVideo = sd(noVideo$likes);
shortVideo = mydata[mydata$video == "Short",];
meanShortVideo = mean(shortVideo$likes);
sdShortVideo = sd(shortVideo$likes);
mediumVideo = mydata[mydata$video == "Medium",];
meanMediumVideo = mean(mediumVideo$likes);
sdMediumVideo = sd(mediumVideo$likes);
longVideo = mydata[mydata$video == "Long",];
meanLongVideo = mean(longVideo$likes);
sdLongVideo = sd(longVideo$likes);
library("ggplot2");
plot_data <- data.frame(
Video = c("No", "Short", "Medium", "Long"),
MeanLikes = c(meanNoVideo, meanShortVideo, meanMediumVideo, meanLongVideo),
SD = c(sdNoVideo, sdShortVideo, sdMediumVideo, sdLongVideo)
);
boxplot(longVideo$likes, mediumVideo$likes, shortVideo$likes,
noVideo$likes,
main = "Variance of likes",
xlab = "Type of video",
ylab = "Amount of likes",
names = c("Long", "Medium", "Short", "No video"),
col = c("red", "blue", "green","yellow" ))
- Are the means different? Yes they
are
- Are the difference among means is statistically significant?
In the boxplots we can see that differences among means are
statistically significant for all pairs except medium and long.
(1 point)
Formulate H0 and Ha. Determine a
significance level, for example \(\alpha=0.05\).
\(H_0\): Means of different video types like
values are equal
\(H_a\): Means
of different video types like values are not equal
(1 point)
library(car)
leveneTest(likes ~ video, data = mydata)
library(rstatix)
mydata %>%
group_by(video) %>%
shapiro_test(likes)
mydata %>%
group_by(video) %>%
identify_outliers("likes")
Give comments about each of test verified.
1. The Levene’s test is not significant because the p-value
0.03218 is greater than the significance level 0.05. Therefore there is
no homogeneity of variances.
2. We can state that all values are
normally distributed because all of the p-values are greater than tye
significance level 0.05
3. Looking at the boxplot provided above
we can see that there are no extreme outliers. We can also check that
with an is.extreme() function
Then, make a final decision what
procedure (parametric or non-parametric) will be applied for further
analysis.
Student’s comments: Because the ANOVA assumptions are
not all met met: there is no homogeneity of variances, all values are
normally distributed and there are no extreme outliers, we will apply a
non-parametric procedure.
(1 point)
res.welch <- oneway.test(likes ~ video, data = mydata, var.equal = FALSE)
print(res.welch)
##
## One-way analysis of means (not assuming equal variances)
##
## data: likes and video
## F = 2461, num df = 3.00, denom df = 337.47, p-value < 2.2e-16
Formulate H0 and Ha. Determine a
significance level \(\alpha=0.05\).
\(H_0\): Means of different video
types are equal
\(H_a\): Means of
different video types are not equal
Student’s comments: the
p-value 2e-16 is less than the significance level 0.05 so we reject the
null hypothesis and conclude that there are significant differences
between the groups highlighted with “*” in the model summary
(1 point)
Decide whether it makes sense to perform multiple pairwise-comparisons for all factors you have in the sample. If yes - perform multiple pairwise-comparisons and make final decision about the differences among groups. If not - just give comments why this step is not relevant.
pairwise.wilcox.test(mydata$likes, mydata$video, p.adjust.method = "BH")
##
## Pairwise comparisons using Wilcoxon rank sum test with continuity correction
##
## data: mydata$likes and mydata$video
##
## Long Medium No
## Medium 0.13 - -
## No <2e-16 <2e-16 -
## Short <2e-16 <2e-16 <2e-16
##
## P value adjustment method: BH
Student’s comments: it made sense to perform multiple pairwise-comparisons for all factors because in the Welch ANOVA test p-value was less than the significance level and null hypothesis was rejected, that indicates that the group means are different. Looking at the results we can see that all pairs besides Medium-Long are statistically significant because the adjusted p-value 0.13 is bigger than the significance level 0.05 and we cannot reject the null hypothesis in this case.