#### Computer Lab 1 ####
# Read data into R
claim.amounts = scan(file=”claim_amounts_data.txt”,skip=2)
# we can see what scan does
?scan
#
data.entry(claim.amounts)
de(claim.amounts)
#### Numerical Summaries ####
summary(claim.amounts)
mean(claim.amounts)
median(claim.amounts)
quantile(claim.amounts)
var(claim.amounts)
sd(claim.amounts)
sd(claim.amounts2)
colnames(claim.amounts2)<-"claims" quantile(claim.amounts,prob=0.75)# third quartile sort(claim.amounts) #which one does R use? claim.amounts[5] claim.amounts[191:200] # compute coefficient of symmetry sum.dev3 = sum( (claim.amounts - mean(claim.amounts))^3 ) beta1 = (sum.dev3/length(claim.amounts))/(sd(claim.amounts))^3 cat("This gives:", "\n", "beta_1 =", beta1, "\n") new.sum.dev3 = 0 # initialise variable to hold the sum dev3 = rep(0,length(claim.amounts)) # initialise array of cubed deviations for (i in 1:length(claim.amounts)){ # start loop dev3[i] = (claim.amounts[i] - mean(claim.amounts))^3 # compute sum new.sum.dev3 = new.sum.dev3 + dev3[i] # iteratively } print(new.sum.dev3) #### Graphical summaries #### group.ind = rep(c(1:2),100); group.ind # create index by # repeating (1,2) 100 times group.1 = claim.amounts[group.ind == 1] # choose amounts with index = 1 group.2 = claim.amounts[group.ind == 2] # same for index = 2 # boxplot boxplot(group.1,group.2,horizontal=T,col="orange",names=c("Group 1","Group 2")) title(main="Boxplots of claim amounts per group",xlab="Amount (1000s pounds)") # alternatively, we can use the following command boxplot(claim.amounts ~ group.ind,horizontal=T,col="orange", names=c("Group 1","Group 2")) par(mfrow=c(1,2)) # Specifies layout for plotting window amount.breaks = seq(0.9,2.3,by=0.1) hist(group.1, breaks=amount.breaks, right=FALSE, col="gray", xlim=c(0.9,2.3), ylim=c(0,25), main="Claim amounts: Group 1", xlab="Amount (1000)") hist(group.2, breaks=amount.breaks, right=FALSE, col="gray", xlim=c(0.9,2.3), ylim=c(0,25), main="Claim amounts: Group 2", xlab="Amount (1000)") #### writing our own function #### beta1.fn <- function(x){ # name and input of function sum.dev3 = sum( (x - mean(x))^3 ) # see Lab 1 ... b1 = (sum.dev3/length(x))/(sd(x))^3 cat("\n","Coefficient of symmetry for these data is: ", "beta_1 =", b1, "\n\n") return(b1)} beta1.fn(claim.amounts) b1<-beta1.fn(claim.amounts) # Suppose that we change the data claim.amounts.new = 0.6 + claim.amounts claim.amounts.new2 = 1.3 * claim.amounts beta1.fn(claim.amounts.new) beta1.fn(claim.amounts.new2)