###-----------------loading data-----------------

data() # list of data sets that are available in base packages 

data(cars) # load "cars" data 

?cars # information about the data 
# there are two variables: speed and stopping distance that discribe 50 observations/cases/cars

cars # prints out the data

class(cars) # checks the data class

dim(cars) # shows dimensions of the data object
# 2 dimensional object: 50 rows and 2 columns

colnames(cars) <- c("speed", "stop_dist") # adds colnames (variable names)

View(cars) # displays data

###-----------------central tendency measures-----------------

mean(cars) # mean calculation
# does not work for a whole data.frame 

mean(cars[,1]) # calculates mean of the first column/variable (speed)

speed <- cars[,1] # saves first column into a data object (vector)
speed
distance <- cars[,2] # saves second column into a data object (vector)
distance

mean(speed) # calculates mean 

length(speed) # shows a length (number of elements) of the object

sum(speed) # sums all elements of the object

sum(speed) / length(speed) # calculates mean

median(speed) # calculates median

n <- (length(speed) + 1) / 2 # identifies middle-ranked element in the object: n + 1 / 2, where n = number of elements
n # prints out the result

speed[n] # chooses nth (25.5th) element of the object = 15

# position measures

quantile(speed, c(0.25, 0.5, 0.75)) # calculates lower, middle, and upper quartile
IQR(speed) # calculates interquartile range: Q3 - Q1

min(speed) # minimal value in the object (vector)
max(speed) # maximal value in the object (vector)

###-----------------variability measures-----------------

range(speed) # show range of the vector

var(speed) # calculates variance: (sum (x - mean)^2) / (n - 1)

speed - mean(speed) # calculates deviations: difference of obervations from the mean

deviations <- speed - mean(speed) # save deviations to the data object (vector)
deviations

round(sum(deviations), 0) # sum of rounded deviations = 0

sum.squared.deviations <- sum((speed - mean(speed))^2) # sum of squared deviations
sum.squared.deviations

sum.squared.deviations / (length(speed) - 1) # calculates variance: SSD / n - 1 

sd(speed) # calculates std. deviation = square root of variance / n - 1

sqrt(sum.squared.deviations / (length(speed) - 1)) # square root of variance / n - 1

scale(speed) # calculates z-scores = deviation / std. dev. 

(speed - mean(speed)) / sd(speed) # deviation / std. dev. 

sd(speed) / mean(speed) # calculates coefficient of variability = std. dev. / mean 

###-----------------graphic displays of descriptive statistics----------------- 

# creats a new vector of brands (brand variable)
brand <- c(rep("Mercedes", 5), rep("Skoda", 9), rep("Ferrari", 1), rep("Tata", 23), 
           rep("Ford", 10), rep("Lada", 2))

cars <- as.data.frame(cbind(cars, brand)) # column binds the brand vector to the cars data.frame

View(cars)

counts <- table(cars[,3]) # counts frequencies of car brands = frequencies of variable categories
counts # brand variable has 6 categories/values (Ferrari, Ford, Lada, Mercedes, Skoda, Tata)

# barplot: frequency of cars (categorical variable)

barplot(counts,                                # data 
        main="Barplot of car frequencies",     # main title
        xlab="brands",                         # X-axis label
        ylab="frequency")                      # Y-axis label
           
# histogram: frequency of speeds (quantitative variable)

hist(speed, main="Histogram of speed frequencies", xlab="speed", ylab="frequency", 
     col="lightblue") # color of bars

# boxplot: distribution of speeds (quantitative variable)

boxplot(speed, ylab="speed", main="Boxplot of speed", col="lightgreen")

# plot: observations/cars placed according to their speed (X) and stopping distance (Y) values

plot(speed,    # X-axis: speed variable 
     distance, # Y-axis: stopping distance variable
     main="Speed over stoping distance", xlab="speed", ylab="stopping distance")  

text(speed, distance,        # data: X and Y coordinates of the observations
     labels=rownames(cars),  # names of observations defined as row.names of the data.frame
     cex=0.75)               # font size

plot(speed, distance, main="Speed over stoping distance", xlab="speed", ylab="stopping distance",
     type="n")  # removes data points from the plot - name labels are better readable

text(speed, distance, labels=rownames(cars), cex=0.75)

abline(reg=lm(distance ~ speed), # fits regression line (shows a relationship between the variables)
       col="red")                # color of the line
