##Exercise 1
Level of measurement
library(knitr)
levels_table <- data.frame(
Level_of_Measurement = c("Nominal", "Ordinal", "Ratio"),
Examples = c(
"Gender, Hair color, Blood type",
"Education level, Satisfaction scale, Class rank",
"Height, Weight, Age, Income"
),
Description = c(
"Categories used as labels only; no ranking or order possible",
"Ordered categories; ranking matters but differences are not measurable",
"Ordered, equal intervals, and has a true zero; allows meaningful ratios"
),
Operations = c(
"= (equal / not equal), Counting frequencies",
"=, !=, <, >, Ranking",
"=, !=, <, >, +, -, *, /, Ratios"
)
)
kable(levels_table, caption = "Levels of Measurement Table")
| Level_of_Measurement | Examples | Description | Operations |
|---|---|---|---|
| Nominal | Gender, Hair color, Blood type | Categories used as labels only; no ranking or order possible | = (equal / not equal), Counting frequencies |
| Ordinal | Education level, Satisfaction scale, Class rank | Ordered categories; ranking matters but differences are not measurable | =, !=, <, >, Ranking |
| Ratio | Height, Weight, Age, Income | Ordered, equal intervals, and has a true zero; allows meaningful ratios | =, !=, <, >, +, -, *, /, Ratios |
Import Students.txt into R
students<-read.delim("C:\\Users\\ahmad\\Downloads\\Datasets\\students.txt",stringsAsFactors=F)
Call library mosaic
library(mosaic)
## Registered S3 method overwritten by 'mosaic':
## method from
## fortify.SpatialPolygonsDataFrame ggplot2
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected by this.
##
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following object is masked from 'package:Matrix':
##
## mean
## The following object is masked from 'package:ggplot2':
##
## stat
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
## quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
##Exercise 6
Creating the table:
tab <- matrix(c(66, 53, 28, 28, 17,11,3), ncol=1, byrow=TRUE)
colnames(tab) <- c("Frequency")
rownames(tab) <- c("Industry","Master student","Acadamic Faculty","Researcher","Doctoral Student","Undergraduate Student","Non-profit")
tab <- as.table(tab)
Creating a data frame:
ex6 <- data.frame(Frequency = c(66, 53, 28, 28, 17,11,3), Background
= c("Industry","Master student","Acadamic Faculty","Researcher","Doctoral Student","Undergraduate Student","Non-profit"))
Creating a barchart:
par(mar = c(8, 5, 4, 2))
barplot(
ex6$Frequency,
names.arg = ex6$Background,
col = rainbow(7),
las = 2, # Rotate labels vertically for readability
cex.names = 0.8, # Adjust label size
main = "Frequency by Background",
ylab = "Frequency",
ylim = c(0, 80) # Add height space so labels fit
)
legend("topright", legend = ex6$Background, fill = rainbow(7), cex = 0.8)
Creating a barchart using ggplot2
library(ggplot2)
ggplot(ex6, aes(x = Background, y = Frequency, fill = Background)) +
geom_col(width = 0.7, show.legend = FALSE) +
coord_flip() + # Flip chart to make long labels readable
labs(title = "Frequency by Background", x = "Background", y = "Frequency") +
theme_minimal(base_size = 12) +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 10)
)
##Exercise 7
Absolute frequencies
tally(~Blood_group, data=students)
## Blood_group
## 0 A AB B
## 31 35 5 11
Relative frequencies
prop(~Blood_group, success = "0", data = students)
## prop_0
## 0.3780488
prop(~Blood_group, success = "A", data = students)
## prop_A
## 0.4268293
prop(~Blood_group, success = "AB", data = students)
## prop_AB
## 0.06097561
prop(~Blood_group, success = "B", data = students)
## prop_B
## 0.1341463
Create the pie chart using the absolute frequencies
blood_pie_ex7 <- c(31,35,5,11)
Pie chart
pie(blood_pie_ex7, labels = c("Blood group 0", "A", "AB", "B"))
##Exercise 8
Bar chart of the variable ’points reached in the exam’
bargraph(~Points_exam, data=students)
##Exercise 9
Histogram of the variable ’body size’
histogram(~Size_cm, data = students)
##Exercise 10
Absolute frequencies
tally(~Grade, data=students)
## Grade
## 1 2 3 4 5
## 14 12 27 8 21
Relative frequencies
prop(~Grade, success = "1", data = students)
## prop_1
## 0.1707317
prop(~Grade, success = "2", data = students)
## prop_2
## 0.1463415
prop(~Grade, success = "3", data = students)
## prop_3
## 0.3292683
prop(~Grade, success = "4", data = students)
## prop_4
## 0.09756098
prop(~Grade, success = "5", data = students)
## prop_5
## 0.2560976
Create the pie chart using the absolute frequencies
blood_pie_ex10 <- c(14, 12, 27, 8, 21)
Pie chart
pie(blood_pie_ex10, labels = c("Grade 1", "2", "3", "4","5"))
Bar chart of the variable “Grade”
bargraph(~Grade, data=students)
##Exercise 11
Histogram of the variable ’body weight’
histogram(~Weight_kg, data = students)