Add loss function practicve

This commit is contained in:
2024-01-10 15:16:52 +02:00
parent a27137a2b1
commit 52a300cd77

86
loss.R Normal file
View File

@@ -0,0 +1,86 @@
# Install datawizard package (for calculating mode with 'distribution_mode()')
if (!require(datawizard)) {
install.packages("datawizard")
library(datawizard)
}
# Define the Salaries vector
Salaries <- c(12567, 15400, 11345, 13130, 12567, 12812, 14908)
# Calculate the mode
modeS <- distribution_mode(Salaries)
modeS
# Calculate the median
meadianS <- median(Salaries)
meadianS
# Calculate the mean
meanS <- mean(Salaries)
meanS
# The first loss function ---------------------
## For each value in vector "Salaries", check whether it is:
#+ 1. different from the mode
#+ 2. different from the median
#+ 3. different from the mean
#+ ...And sum up the results (the "True" results)
# How many values are different from the mode?
# (how many people make something other than the mode?)
sum(Salaries != modeS)
# How many people make something other than the median?
sum(Salaries != meadianS)
# Etc
sum(Salaries != meanS)
# Second loss function ----------------------------
sum( abs (Salaries - modeS))
# ^ summarize the ^ absolute value of ^ each salary minus the modal salary
sum(abs(Salaries - meadianS))
sum(abs(Salaries - meanS))
# Third loss function ----------------------------
sum((Salaries - modeS)^2)
sum((Salaries - meadianS)^2)
sum((Salaries - meanS)^2)
#..........................................Exercise 2
#Create a vector of numbers
potato <- runif(n=20, min=1, max=20)
#Calculate the mean, median and mod of the vector of numbers you created
potato_mean <- mean(potato)
potato_mean
potato_median <- median(potato)
potato_median
potato_mode=distribution_mode(potato)
potato_mode
# print results
sprintf("Mean %s Median %s Mode %s", potato_mean, potato_median, potato_mode)
#Calculate the error for each loss function for each of the
#measures you calculated (the mean, median and mod)
# 1st loss function
sum(potato != potato_mode)
# 2nd loss function
sum(abs(potato - potato_median))
# 3rd loss function
sum((potato - potato_mean)^2)