Dear R helpers

exposure <- data.frame(id = 
c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20),
ead = c(9483.686,50000,6843.4968,10509.37125,21297.8905,50000,706152.8354, 
62670.5625, 687.801995,50641.4875,59227.125,43818.5778,52887.72534,601788.7937, 
56813.14859,4012356.056,1419501.179,210853.4743,749961,6599.0862),
pd = c(0.0191,0.0050,0.0298,0.0449,0.0442,0.0479,0.0007,0.0203,0.0431,0.0069, 
0.0122,0.0022,0.0016,0.0082,0.0109,0.0008,0.0142,0.0171,0.0276,0.0178),
lgd = 
c(0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,
 0.45,0.45,0.45,0.45))

param <- data.frame(alpha = 0.99, size = 50)    # size is basically no of 
simulations


n          <-
 length(exposure$id)
id         <- exposure$id
ead        <- exposure$ead
lgd        <- exposure$lgd
pd         <- exposure$pd 
alpha      <- param$alpha
samplesize <- param$size

## generate random numbers s.t. 1 = Default, 0 = no-default. 

L <- matrix(data=NA, nrow=n, ncol=samplesize, byrow=TRUE)

for(i in 1:n)
    L[i,] <- rbinom(n=samplesize, size=1, prob=exposure$pd[i])

# ________________________________________________________________

# compute for each simulation

p_loss <- e_loss <- u_loss <- NULL

for(i in 1:samplesize)

{
  
defaulting <- subset(data.frame(id=exposure$id, ead=exposure$ead, 
lgd=exposure$lgd, pd=exposure$pd, loss=L[,i]),
 loss==1)

p_loss[i]  <- sum(defaulting$ead * defaulting$lgd)
e_loss[i]  <- sum(defaulting$ead * defaulting$lgd * defaulting$pd)
u_loss[i]  <- sum(sqrt((defaulting$ead*defaulting$lgd)^2*defaulting$pd - 
(defaulting$ead * defaulting$lgd * defaulting$pd)^2))

sim_data   <- data.frame(sim_no=rep(i,length(defaulting$id)), id=defaulting$id, 
ead=defaulting$ead, lgd=defaulting$lgd, pd=defaulting$pd)

write.csv(sim_data, file='sim_data.csv', append=TRUE, row.names=FALSE)

}

For a given set of 0's and 1's (i.e. for each simulation and there are 50 
simulations), first I filter all the entries corresponding to 0's i.e. for a 
given simulation, I need to store ead, lgd and pd pertaining to only non-zeros 
i.e. pertaining to 1. Thus, for each of these 50 simulations, I need to define 
a data.frame giving me filtered ead, lgd and pd and in teh end write a single 
file sim_data.csv

I get
 following warnings.

Warning messages:
1: In write.csv(sim_data, file = "sim_data.csv", append = TRUE,  ... :
  attempt to set 'append' ignored
2: In write.csv(sim_data, file = "sim_data.csv", append = TRUE,  ... :
  attempt to set 'append' ignored
.....................................................................................................

.....................................................................................................
50: In write.csv(sim_data, file = "sim_data.csv", append = TRUE,  ... :

  attempt to set 'append' ignored

Kindly guide

Regards

Vincy




        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to