2016-07-31 5 views
1

Ich habe eine Matrix, die die Ausgabe der EuropeanOptionArrays-Funktion im QuantLib-Paket war. Das ist die Struktur:Matrix zu Dataframe für einfachere 3D-Darstellung

num [1:61, 1:33] 0.0109 0.0154 0.0215 0.0298 0.0409 ... 
- attr(*, "dimnames")=List of 2 
..$ : chr [1:61] "600" "605" "610" "615" ... 
..$ : chr [1:33] "0.128767123287671" "0.124798869319417" "0.12083061535116.. 

Ich mag würde einen 2013 x 3 Datenrahmen anstelle der oben zu schaffen. Ich möchte es so aussehen:

Strike Price   Time to Maturity  Option Value 
    650     .1287     
    650     .1247 
    650     .1208 
    650     .1168 
    ...     ...     .... 

Statt wie es aussieht, jetzt, wo die Optionswerte sind nicht Vektoren:

   0.1287   0.1247   0.1208   .1168... 
600  0.01089619  0.009433598  0.006943248  0.005897702  
605  0.01539911  0.013421805  0.010021222  0.008577718  
610  0.02153793  0.018894224  0.014303551  0.
615  0.02982046  0.026323863  0.020195542  0.017539548  
620  0.04088268  0.036306751  0.028214722  0.024673190  
625  0.05551221  0.049585461  0.039014121  0.034343843 
... 

Gibt es eine einfache Lösung für dieses?

Dank

+0

sorry 2013x3 Datenrahmen. 2013 Zeilen, 3 Spalten –

+1

Perfekt. Ich habe die Persp-Funktion mit Matrix verwendet, aber ich wollte die Daten auf diese Weise im rgl-Paket mitspielen. Vielen Dank! –

Antwort

0

Wenn Sie Matrix mat ist, können Sie eine der beiden wählen:

dat1 <- cbind(expand.grid(Strike_Price = as.numeric(rownames(mat)), 
          Time_to_Maturity = as.numeric(colnames(mat))), 
       Option_Value = as.numeric(mat)) 

dat2 <- cbind(expand.grid(Time_to_Maturity = as.numeric(colnames(mat)), 
          Strike_Price = as.numeric(rownames(mat))), 
       Option_Value = as.numeric(t(mat))) 

Beispiel

set.seed(0) 
mat <- matrix(rnorm(12), 3, 4) 
rownames(mat) <- 1:3 
colnames(mat) <- 11:14 

#   11   12   13   14 
#1 1.2629543 1.2724293 -0.928567035 2.4046534 
#2 -0.3262334 0.4146414 -0.294720447 0.7635935 
#3 1.3297993 -1.5399500 -0.005767173 -0.7990092 

Mein Code gibt:

## dat1 

# Strike_Price Time_to_Maturity Option_Value 
#1    1    11 1.262954285 
#2    2    11 -0.326233361 
#3    3    11 1.329799263 
#4    1    12 1.272429321 
#5    2    12 0.414641434 
#6    3    12 -1.539950042 
#7    1    13 -0.928567035 
#8    2    13 -0.294720447 
#9    3    13 -0.005767173 
#10   1    14 2.404653389 
#11   2    14 0.763593461 
#12   3    14 -0.799009249 

## dat2 

# Time_to_Maturity Strike_Price Option_Value 
#1    11   1 1.262954285 
#2    12   1 1.272429321 
#3    13   1 -0.928567035 
#4    14   1 2.404653389 
#5    11   2 -0.326233361 
#6    12   2 0.414641434 
#7    13   2 -0.294720447 
#8    14   2 0.763593461 
#9    11   3 1.329799263 
#10    12   3 -1.539950042 
#11    13   3 -0.005767173 
#12    14   3 -0.799009249 

Wie ich in meinem Kommentar sagte, bin ich nicht sicher, warum Sie einen Datenrahmen hier wollen. Ich würde mir eine Matrix vorstellen, die perfekt für 3D-Plot ist. Zum Beispiel:

Strike_Price <- as.numeric(rownames(mat)) 
Time_to_Maturity <- as.numeric(colnames(mat)) 
persp(Strike_Price, Time_to_Maturity, mat) 
##image(Strike_Price, Time_to_Maturity, mat) 
##contour(Strike_Price, Time_to_Maturity, mat) 
Verwandte Themen