If you want to see the output of the block codes in the knitted document, you need to change the chunk options to {eval = TRUE}. Or you could simply just run it in RStudio.
You can use R as a calculator through the script or directly input it to the console.
1+4
## [1] 5
39*24
## [1] 936
sqrt(193)
## [1] 13.89244
log(10) # default base is e, i.e. it is ln()
## [1] 2.302585
log(10, base = 10)
## [1] 1
exp(10) # also base e, i.e. e^(10) this is
## [1] 22026.47
More realistic use of R….. variables. You can use <- or = or -> to assign values, and variable names are case-sensitive.
Note: “tax rate” will result error in variable naming.
premium <- 1200
tax_rate <- 0.13
total_premium <- premium * (1 + tax_rate)
total_premium
You can use the ?function and help(function) to get more information of the function you wish to use
?qnorm # this is the quantile function for normal distribution
help(qnorm) # same way of getting the documentation page for a function, dataset etc.
args(qnorm) # display the function's arguments
example(mean) # just to show you what the function does
You will need to have the file to make this code works.
# Comma-separated file
claims <- read.csv("claims.csv")
# Text file with a header row
rates <- read.table("rates.txt", header = TRUE)
# Excel file
library(readxl)
policies <- read_excel("policies.xlsx")
ages <- c(18, 21, 19, 20, 21)
ages # just a printout of the variable ages - a "list"
ages[1] # first element in the "list"
length(ages)
mean(ages)
ages + 1 # add one to each of the values in the "list"
ages >= 20 # doing evaluation on each of the entries in the list
ages[ages >= 20] # only taking the entries that is true under the condition given
# ways of creating sequence or manipulate the entries
1:10 # list of [1,2,3,4,5,6,7,8,9,10]
seq(from = 0, to = 1, by = 0.2) # 5 elements
seq(from = 5, to = 25, length.out = 5)
rep(1:3, times = 2)
rep(c("A", "B"), each = 3)
ages <- c(18, 21, 19, 20, 21)
ages # just a printout of the variable ages - a "list"
ages[1] # first element in the "list"
length(ages)
mean(ages)
ages + 1 # add one to each of the values in the "list"
ages >= 20 # doing evaluation on each of the entries in the list
ages[ages >= 20] # only taking the entries that is true under the condition given
loss_ratio <- function(loss, premium) {
ratio <- loss / premium
return(ratio)
}
loss_ratio(9000, 14000) # 0.6428571
loss_ratio(18000, 14000) # 1.285714
# Install if the package is not already installed
# install.packages("ggplot2")
# Load for use in the current R session
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
demo <- data.frame(
month = 1:6,
count = c(4, 7, 6, 9, 5, 8),
total = c(6000, 10500, 7200, 15100, 6500, 12700),
budget = rep(10000, 6)
)
demo$count
## [1] 4 7 6 9 5 8
mean(demo$count)
## [1] 6.5
demo[demo$count > 6, ] # gives you the rows that count > 6
## month count total budget
## 2 2 7 10500 10000
## 4 4 9 15100 10000
## 6 6 8 12700 10000
plot(demo$count, demo$total, main = "Claim count and total cost", xlab = "Number of claims", ylab = "Total cost (CAD)", pch = 19, col = "steelblue", cex = 1.3)
# note: col is for color not column, or just use ?plot to look up the arguments
plot(demo$month, demo$count, type = "p") # p for points
plot(demo$month, demo$count, type = "l") # l for line
plot(demo$month, demo$count, type = "b") # b for both points and line
# plot with legend
plot(demo$month, demo$total, type = "b", pch = 19, lwd = 2, col = "steelblue",xlab = "Month", ylab = "Amount (CAD)", ylim = c(0, 18000))
lines(demo$month, demo$budget, col = "firebrick", lty = 2, lwd = 2)
legend("topleft", c("Cost", "Budget"), col = c("steelblue", "firebrick"), lty = c(1, 2), lwd = 2, bty = "n")
set.seed(613)
scores <- rnorm(500, mean = 100, sd = 15)
hist(scores, breaks = 10, probability = TRUE,
border = "steelblue", xlab = "Score", ylim = c(0, 0.035), col = "lightblue")
# change breaks value to change the bin count
curve(dnorm(x, mean = 100, sd = 15),
from = 40, to = 160, add = TRUE,
col = "firebrick", lwd = 2)
policy_type <- c(rep("Auto", 18), rep("Home", 12), rep("Travel", 6))
type_count <- table(policy_type) # counts the observations in each category
barplot(type_count, main = "Policies by type", xlab = "Policy type", ylab = "Number of policies", col = "steelblue")
# grouped barplot
sales <- matrix(c(12, 18, 15, 7, 11, 9), nrow = 2, byrow = TRUE)
rownames(sales) <- c("Auto", "Home")
colnames(sales) <- c("East", "West", "Central")
barplot(sales, beside = TRUE, col = c("steelblue", "orange"), legend.text = rownames(sales), ylab = "Number of policies")
pie(type_count, labels = names(type_count), main = "Policy mix", col = c("lightblue", "wheat", "thistle"))
round(100 * type_count / sum(type_count), 1)
## policy_type
## Auto Home Travel
## 50.0 33.3 16.7
curve(dnorm(x), from = -4, to = 4, ylab = "Density")
curve(pnorm(x), from = -4, to = 4, ylab = "Probability")
Create claims and a monthly loss ratio column. Report annual totals, mean count, and annual loss ratio. Select months with loss above premium and use an if statement for the annual result.
claims <- read.csv("~/Library/Mobile Documents/com~apple~CloudDocs/UW_2026/613_Stats/monthly_claims.csv")
# you could use (file.choose()) instead of path
claims$loss_ratio <- claims$loss/claims$premium
claims
## month count loss premium loss_ratio
## 1 1 6 9000 14000 0.6428571
## 2 2 9 14500 14000 1.0357143
## 3 3 5 6500 14000 0.4642857
## 4 4 11 18500 14000 1.3214286
## 5 5 8 11000 14000 0.7857143
## 6 6 7 10500 14000 0.7500000
## 7 7 10 17000 14000 1.2142857
## 8 8 4 5500 14000 0.3928571
## 9 9 9 14000 14000 1.0000000
## 10 10 12 22000 14000 1.5714286
## 11 11 6 8500 14000 0.6071429
## 12 12 8 13000 14000 0.9285714
annual_claims_total <- sum(claims$count)
annual_loss_total <- sum(claims$loss)
annual_premium_total <- sum(claims$premium)
average_count <- mean(claims$count)
annual_loss_ratio <- annual_loss_total/annual_claims_total
months_above_premium <- claims[claims$loss >claims$premium,] # compare for each row
months_above_premium
## month count loss premium loss_ratio
## 2 2 9 14500 14000 1.035714
## 4 4 11 18500 14000 1.321429
## 7 7 10 17000 14000 1.214286
## 10 10 12 22000 14000 1.571429
if(annual_loss_total > annual_premium_total){
print("The total annual loss exceed the annual premium total")
}else{
print("The total annual loss did not exceed the annual premium total")
}
## [1] "The total annual loss did not exceed the annual premium total"
Plot monthly loss with a premium reference line and legend. Make a scatter plot with claim count on the x-axis and total loss on the y-axis. Add units and write a sentence for each plot.