Dear R helpers,

I know R language at a preliminary level. This is my first post to this R
forum. I have recently learned the use of function and have been successful
in writing few on my own. However I am not able to figure out how to apply
the function to multiple sets of data.

# MY QUERY

Suppose I am having following data.frame

df = data.frame(k = c(1:8), ratings = c("A", "B", "C", "D", "E", "F", "G",
"H"),
default_frequency =
c(0.00229,0.01296,0.01794,0.04303,0.04641,0.06630,0.06862,0.06936))

# -------------------------------

DP = function(k, ODF, ratings)

{

n                   <-  length(ODF)
tot_klnODF    <-  sum(k*log(ODF))
tot_k             <-  sum(k)
tot_lnODF     <-  sum(log(ODF))
tot_k2           <-  sum(k^2)
slope            <-  exp((n * tot_klnODF - tot_k * tot_lnODF)/(n * tot_k2 -
tot_k^2))
intercept       <-  exp((tot_lnODF - log(slope)* tot_k)/n)
IPD               <-  intercept * slope^k

return(data.frame(ratings = ratings, default_probability = round(IPD, digits
= 4)))

}

result = DP(k = df$k, ODF = df$default_frequency, ratings = df$ratings)

#
________________________________________________________________________________________

The above code fetches me following result. However, I am dealing with only
one set of data here as defined in 'df'.

> result

  ratings default_probability
1       A              0.0061
2       B              0.0094
3       C              0.0145
4       D              0.0222
5       E              0.0342
6       F              0.0527
7       G              0.0810
8       H              0.1247


# MY PROBLEM

Suppose I have data as given below

Class            k      rating      default_frequency
Bank            1         A            0.00229
                   2         B             0.01296
                   3         C             0.01794
                   4         D             0.04303
                   5         E             0.04641
                   6         F             0.06630
                  7         G             0.06862
                  8         H             0.06936
Corporate    1         A             0.00101
                  2         B             0.01433
                  3         C             0.02711
                  4         D             0.03701
                  5         E             0.04313
                  6         F             0.05600
                  7         G             0.06041
                  8         H             0.07112
Sovereign    1         A             0.00210
                  2         B             0.01014
                  3         C             0.02001
                  4         D             0.04312
                  5         E             0.05114
                  6         F             0.06801
                  7         G             0.06997
                  8         H             0.07404

So I need to use the function "DP" defined above to generate three sets of
results viz. for Bank, Corporate, Sovereign and save each of these results
as diffrent csv files say as bank.csv, corporate.csv etc. Again please note
that there could be say 'm' number of classes. I was trying to use the apply
function but things are not working for me. I will really apprecaite the
guidenace. I hope I am able to put up my query in a neat manner.

Regards and thanking you all in advance.

Akshata Rao

        [[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