Reputation: 934
Need help! Keep getting this error:
Error: object 'freqSim' not found
From this portion of the code:
indfreqDF <- function(x){
frame <- severity[severity$industryType == x,]
weightPercent <- frame$relfreq
z <- largeAvgFunc2(freqDF)*weightPercent
z <- z[-c(8)]
# run a for loop to simulate 0/1 dataframe for each loss type
for (i in 1:7){
freqSim[i] <<- data.frame(sample(0:1, length(1:10000), replace=T, prob = c(1-z[i],z[i])))
}
return(freqSim[i])
}
indfreqDF("Software")
Error in freqSim[i] <<- data.frame(sample(0:1, length(1:10000), replace = T, :
object 'freqSim' not found
Ideally, what I would like is a 10,000x7 (row by col) dataframe/matrix of the 7 different unique damage types (damageType column, found within code below), with 10,000 randomly selected 0/1 values.
Code to Recreate:
dec_amt <- function(x, k) format(round(x, k), nsmall=k)
severity <- data.frame(industryType = c("Consumer Products",
"Biotech/Pharma",
"Industrial/Construction",
"Computer Hardware/Electronics",
"Medical Devices",
"Software",
"Business/Consumer Services",
"Telecommunications",
"Automotive/Transportation",
"Chemicals/Synthetic Materials",
"All Industries"),
relfreq = c(2.032520,
0.650407,
1.327913,
1.571816,
1.463415,
0.758808,
0.623306,
0.650407,
0.460705,
0.460705,
1.000000),
relsev = c(0.419048,
3.771429,
0.609524,
2.019048,
3.028571,
1.314286,
0.723810,
4.247619,
0.152381,
0.076190,
1.000000))
largeAvgFunc2 <- function(x) {
three <- apply(x, 2, function(y) mean(tail(y,10)))
four <- apply(x, 2, function(y) mean(tail(y,5)))
avgMu2 <- apply(cbind(three,four), 1, mean)
return(avgMu2)
}
PIRATE <- data.frame(damageType = c("RR","RR","RR","LP","LP","LP","CD","CD","CD","ED","ED","ED","AF","AF","AF","CS","CS","CS","PI","PI","PI","TD","TD","TD")
,Year = c(2000,2001,2002,2000,2001,2002,2000,2001,2002,2000,2001,2002,2000,2001,2002,2000,2001,2002,
2000,2001,2002,2000,2001,2002),
Count = c(4,4,9,18,17,20,15,15,12,12,12,12,15,12,12,53,1,3,5,6,7,2,12,6),
Amount = c(35222,12512012,12512,1251,1251251,12151,7771,1091,1091,121,121,194,1512,125125,125,621,
2631,136161,1321236,2136,111,3213,1262,6610))
PIRATE <- transform(PIRATE, medAmount = Amount/Count)
PIRATE <- transform(PIRATE, freqYearly = Count/c(18,
16,
45))
PIRATE$freqYearly <- dec_amt(PIRATE$freqYearly, 4);PIRATE$freqYearly <- as.numeric(PIRATE$freqYearly)
freqDF <- data.frame(PIRATE$damageType, PIRATE$freqYearly, PIRATE$Year)
freqDF <- acast(freqDF, PIRATE.Year ~ PIRATE.damageType, value.var = 'PIRATE.freqYearly')
indfreqDF <- function(x){
frame <- severity[severity$industryType == x,]
weightPercent <- frame$relfreq
z <- largeAvgFunc2(freqDF)*weightPercent
z <- z[-c(8)]
# run a for loop to simulate 0/1 dataframe for each loss type
for (i in 1:7){
freqSim[i] <<- data.frame(sample(0:1, length(1:10000), replace=T, prob = c(1-z[i],z[i])))
}
return(freqSim[i])
}
indfreqDF("Software")
Upvotes: 0
Views: 1410
Reputation: 1023
One possible way to get there would be to change indfreqDF
to:
indfreqDF <- function(x){
frame <- severity[severity$industryType == "Software",]
weightPercent <- frame$relfreq
z <- largeAvgFunc2(freqDF)*weightPercent
z <- z[-c(8)]
# run a for loop to simulate 0/1 dataframe for each loss type
freqSim <- list()
for (i in 1:7){
freqSim[[i]] <- data.frame(sample(0:1, length(1:10000), replace=T, prob = c(1-z[i],z[i])))
}
do.call(cbind, freqSim)
}
Upvotes: 1