R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("fMultivar-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('fMultivar') Loading required package: mgcv This is mgcv 1.3-1 Loading required package: nnet Loading required package: fBasics Rmetrics, (C) 1999-2005, Diethelm Wuertz, GPL fBasics: Markets, Basic Statistics, Date and Time Loading required package: fCalendar Rmetrics, (C) 1999-2005, Diethelm Wuertz, GPL fCalendar: Markets, Basic Statistics, Date and Time Loading required package: fSeries Rmetrics, (C) 1999-2005, Diethelm Wuertz, GPL fSeries: The Dynamical Process Behind Financial Markets Rmetrics, (C) 1999-2005, Diethelm Wuertz, GPL fSeries 2: Multivariate Financial Markets Analysis > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "A1-RegressionModelling" > > ### * A1-RegressionModelling > > flush(stderr()); flush(stdout()) > > ### Name: RegressionModelling > ### Title: Univariate Regression Modelling > ### Aliases: RegressionModelling fREG fREG-class regFit predict.fREG > ### print.fREG plot.fREG summary.fREG fitted.values.fREG residuals.fREG > ### OLS print.OLS plot.OLS summary.OLS > ### Keywords: models > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > ## SOURCE("fMultivar.A1-RegressionModelling") > > ## regFit - > xmpMultivar("\nStart: Load US Recession Modelling Data Set > ") > data(recession) > > ## myPlot - > xmpMultivar("\nNext: Write Internal Plot Function > ") > myPlot = function(recession, in.sample) { + Date = recession[, "date"] + Date = trunc(Date/100) + (Date-100*trunc(Date/100))/12 + Recession = recession[, "recession"] + inSample = as.vector(in.sample) + plot(Date, Recession, type = "n", main = "US Recession") + grid() + lines(Date, Recession, type = "h", col = "steelblue3") + lines(Date, inSample) } > > ## Generalized Additive Modelling: > xmpMultivar("\nNext: Generalized Additive Modelling > ") > require(mgcv) [1] TRUE > par(mfrow = c(2, 2)) > fit = regFit(formula = recession ~ s(tbills3m) + s(tbonds10y), + family = gaussian(), data = recession, method = "GAM") > # In Sample Prediction: > in.sample = predict(fit, newdata = recession)$fit > myPlot(recession, in.sample) > # Summary: > summary(fit) Title: Generalized Additive Modelling Formula: ~ recession s(tbills3m) + s(tbonds10y) Family: gaussian identity Model Parameters: (Intercept) s(tbills3m).1 s(tbills3m).2 s(tbills3m).3 s(tbills3m).4 0.143187 0.166458 -0.013294 0.222052 0.380716 s(tbills3m).5 s(tbills3m).6 s(tbills3m).7 s(tbills3m).8 s(tbills3m).9 0.128114 -0.034126 0.062578 0.657062 -0.154918 s(tbonds10y).1 s(tbonds10y).2 s(tbonds10y).3 s(tbonds10y).4 s(tbonds10y).5 -0.455603 -0.400592 0.091034 -0.529471 0.036311 s(tbonds10y).6 s(tbonds10y).7 s(tbonds10y).8 s(tbonds10y).9 -0.096968 0.001936 -1.372433 0.469935 Residual Variance: 0.07675891 Parametric coefficients: Estimate std. err. t ratio Pr(>|t|) (Intercept) 0.14319 0.01355 10.56 < 2.22e-16 Approximate significance of smooth terms: edf chi.sq p-value s(tbills3m) 7.55 104.58 2.4655e-16 s(tbonds10y) 7.567 84.11 5.6757e-13 R-sq.(adj) = 0.353 Deviance explained = 37.6% GCV score = 0.082618 Scale est. = 0.079542 n = 433 Parametric coefficients: Estimate std. err. t ratio Pr(>|t|) (Intercept) 0.14319 0.01355 10.56 < 2.22e-16 Approximate significance of smooth terms: edf chi.sq p-value s(tbills3m) 7.55 104.58 2.4655e-16 s(tbonds10y) 7.567 84.11 5.6757e-13 R-sq.(adj) = 0.353 Deviance explained = 37.6% GCV score = 0.082618 Scale est. = 0.079542 n = 433 > # Add plots from the original plot method: > gam.fit = fit@fit > class(gam.fit) = "gam" > plot(gam.fit) > > ## POLYMARS Modelling: > xmpMultivar("\nNext: Polymars Modelling - Mode Parameters> ") > par(mfrow = c(3, 2), ask = FALSE) > fit = regFit(formula = recession ~ tbills3m + tbonds10y, + family = gaussian(), data = recession, method = "POLYMARS") > fit Title: Polytochomous MARS Modeling Formula: ~ recession tbills3m + tbonds10y Model Parameters: pred1 knot1 pred2 knot2 coefs SE 1 0 NA 0 NA 2.32863760 0.4916 2 1 NA 0 NA -0.54932042 0.1028 3 1 12.47 0 NA -0.40928942 0.0986 4 1 7.96 0 NA -0.30320408 0.0718 5 1 10.12 0 NA 0.54966250 0.0954 6 2 NA 0 NA -0.41989700 0.0750 7 2 11.86 0 NA 0.42038000 0.1096 8 2 9.17 0 NA 0.40340104 0.0762 9 1 NA 2 NA 0.09842815 0.0152 10 2 7.59 0 NA 0.36797423 0.1601 11 1 NA 2 7.59 -0.13632768 0.0264 > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "A2-RegressionTests" > > ### * A2-RegressionTests > > flush(stderr()); flush(stdout()) > > ### Name: RegressionTests > ### Title: Regression Tests > ### Aliases: RegressionTests lmTest bgTest bpTest dwTest gqTest harvTest > ### hmcTest rainTest resetTest > ### Keywords: htest > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > ## SOURCE("fMultivar.A2-RegressionTests") > > ## bg - > xmpMultivar("\nStart: Breusch-Godfrey Test > ") > # Generate a Stationary and an AR(1) Series: > x = rep(c(1, -1), 50) > y1 = 1 + x + rnorm(100) > # Perform Breusch-Godfrey Test for 1st order serial correlation: > lmTest(y1 ~ x, "bg") Breusch-Godfrey test for serial correlation of order 1 data: formula LM test = 0.0037, df = 1, p-value = 0.9516 > # ... or for fourth order serial correlation: > lmTest(y1 ~ x, "bg", order = 4) Breusch-Godfrey test for serial correlation of order 4 data: formula LM test = 3.0822, df = 4, p-value = 0.5442 > # Compare with Durbin-Watson Test Results: > lmTest(y1 ~ x, "dw") Durbin-Watson test data: formula DW = 1.9762, p-value = 0.4924 alternative hypothesis: true autocorrelation is greater than 0 > y2 = filter(y1, 0.5, method = "recursive") > lmTest(y2 ~ x, "bg") Breusch-Godfrey test for serial correlation of order 1 data: formula LM test = 19.9075, df = 1, p-value = 8.128e-06 > > ## bp - > xmpMultivar("\nStart: Breusch-Pagan Test > ") > # Generate a Regressor: > x = rep(c(-1, 1), 50) > # Generate heteroskedastic and homoskedastic Disturbances > err1 = rnorm(100, sd = rep(c(1, 2), 50)) > err2 = rnorm(100) > # Generate a Linear Relationship: > y1 = 1 + x + err1 > y2 = 1 + x + err2 > # Perform Breusch-Pagan Test > bp = lmTest(y1 ~ x, "bp") > bp studentized Breusch-Pagan test data: formula BP = 14.7875, df = 1, p-value = 0.0001203 > # Calculate Critical Value for 0.05 Level > qchisq(0.95, bp$parameter) [1] 3.841459 > lmTest(y2 ~ x, "bp") studentized Breusch-Pagan test data: formula BP = 0.8683, df = 1, p-value = 0.3514 > > ## dw - > xmpMultivar("\nNext: Durban-Watson Test > ") > # Generate two AR(1) Error Terms > # with parameter rho = 0 (white noise) > # and rho = 0.9 respectively > err1 = rnorm(100) > # Generate Regressor and Dependent Variable > x = rep(c(-1,1), 50) > y1 = 1 + x + err1 > # Perform Durbin-Watson Test: > lmTest(y1 ~ x, "dw") Durbin-Watson test data: formula DW = 1.9072, p-value = 0.3565 alternative hypothesis: true autocorrelation is greater than 0 > err2 = filter(err1, 0.9, method = "recursive") > y2 = 1 + x + err2 > lmTest(y2 ~ x, "dw") Durbin-Watson test data: formula DW = 0.3091, p-value < 2.2e-16 alternative hypothesis: true autocorrelation is greater than 0 > > ## gq - > xmpMultivar("\nNext: Goldfeld-Quandt Test > ") > # Generate a Regressor: > x = rep(c(-1, 1), 50) > # Generate Heteroskedastic and Homoskedastic Disturbances: > err1 = c(rnorm(50, sd = 1), rnorm(50, sd = 2)) > err2 = rnorm(100) > # Generate a Linear Relationship: > y1 = 1 + x + err1 > y2 = 1 + x + err2 > # Perform Goldfeld-Quandt Test: > lmTest(y1 ~ x, "gq") Goldfeld-Quandt test data: formula GQ = 5.1153, df1 = 48, df2 = 48, p-value = 4.309e-08 > lmTest(y2 ~ x, "gq") Goldfeld-Quandt test data: formula GQ = 1.1985, df1 = 48, df2 = 48, p-value = 0.2664 > > ## harv - > xmpMultivar("\nNext: Harvey-Collier Test > ") > # Generate a Regressor and Dependent Variable: > x = 1:50 > y1 = 1 + x + rnorm(50) > y2 = y1 + 0.3*x^2 > # Perform Harvey-Collier Test: > harv = lmTest(y1 ~ x, "harv") > harv Harvey-Collier test data: formula HC = 0.1187, df = 47, p-value = 0.906 > # Calculate Critical Value vor 0.05 level: > qt(0.95, harv$parameter) [1] 1.677927 > lmTest(y2 ~ x, "harv") Harvey-Collier test data: formula HC = 8.0213, df = 47, p-value = 2.368e-10 > > ## hmc - > xmpMultivar("\nNext: Harrison-McCabe Test > ") > # Generate a Regressor: > x = rep(c(-1, 1), 50) > # Generate Heteroskedastic and Homoskedastic Disturbances: > err1 = c(rnorm(50, sd = 1), rnorm(50, sd = 2)) > err2 = rnorm(100) > # Generate a Linear Relationship: > y1 = 1 + x + err1 > y2 = 1 + x + err2 > # Perform Harrison-McCabe Test: > lmTest(y1 ~ x, "hmc") Harrison-McCabe test data: formula HMC = 0.1855, p-value < 2.2e-16 > lmTest(y2 ~ x, "hmc") Harrison-McCabe test data: formula HMC = 0.4213, p-value = 0.147 > > ## rain - > xmpMultivar("\nNext: Rainbow Test > ") > # Generate Series: > x = c(1:30) > y = x^2 + rnorm(30, 0, 2) > # Perform rainbow Test > rain = lmTest(y ~ x, "rain") > rain Rainbow test data: formula Rain = 28.9885, df1 = 15, df2 = 13, p-value = 1.537e-07 > # Compute Critical Value: > qf(0.95, rain$parameter[1], rain$parameter[2]) [1] 2.53311 > > ## reset - > xmpMultivar("\nNext: Reset Test > ") > # Generate Series: > x = c(1:30) > y1 = 1 + x + x^2 + rnorm(30) > y2 = 1 + x + rnorm(30) > # Perform RESET Test: > lmTest(y1 ~ x , "reset", power = 2, type = "regressor") RESET test data: formula RESET = 123136.6, df1 = 1, df2 = 27, p-value < 2.2e-16 > lmTest(y2 ~ x , "reset", power = 2, type = "regressor") RESET test data: formula RESET = 1.7037, df1 = 1, df2 = 27, p-value = 0.2028 > > > > > cleanEx(); ..nameEx <- "A3-EquationsModelling" > > ### * A3-EquationsModelling > > flush(stderr()); flush(stdout()) > > ### Name: EquationsModelling > ### Title: Regression Tests > ### Aliases: EquationsModelling fEQNS fEQNS-class eqnsFit SUR predict.fEQNS > ### print.fEQNS plot.fEQNS summary.fEQNS coef.fEQNS fitted.fEQNS > ### residuals.fEQNS vcov.fEQNS > ### Keywords: models > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > ## SOURCE("fMultivar.A3-EquationsModelling") > > ## Note, "systemfit" is required: > SYSTEMFIT = require(systemfit) Loading required package: systemfit > if (SYSTEMFIT) { + + ## Examples from the 'systemfit' Package: + data(kmenta) + + ## OLS Estimations: + formulas = list(demand = q ~ p + d, supply = q ~ p + f + a ) + FITOLS = eqnsFit(formulas, data = kmenta) + FITOLS + + ## OLS Estimation with 2 Restrictions: + Rrestr <- matrix(0, 2, 7) + qrestr <- matrix(0, 2, 1) + Rrestr[1,3] = 1 + Rrestr[1,7] = -1 + Rrestr[2,2] = -1 + Rrestr[2,5] = 1 + qrestr[2,1] = 0.5 + FITOLS2 = eqnsFit(formulas, data = kmenta, R.restr = Rrestr, + q.restr = qrestr) + FITOLS2 + + ## Iterated SUR Estimation: + FITSUR = eqnsFit(formulas, data = kmenta, method = "SUR", maxit = 100) + FITSUR + # Coefficients, Fitted Values, Residuals and Variance-Covariance Matrix: + # Call by Method: + coef(FITSUR) + fitted(FITSUR) + residuals(FITSUR) + vcov(FITSUR) + + ## 2SLS Estimation: + inst = ~ d + f + a + FIT2SLS = eqnsFit(formulas, data = kmenta, method = "2SLS", inst = inst) + FIT2SLS + # Coefficients, Fitted Values, Residuals and Variance-Covariance Matrix: + # Call by Slot: + FIT2SLS@fit$coef + FIT2SLS@fit$fitted + FIT2SLS@fit$residuals + FIT2SLS@fit$vcov + + ## 2SLS Estimation with Different Instruments in Each Equation: + insts = list( ~ d + f, ~ d + f + a) + FIT2SLS2 = eqnsFit(formulas, data = kmenta, method = "2SLS", inst = insts) + FIT2SLS2 + + ## 3SLS Estimation with GMM-3SLS Formula: + instruments = ~ d + f + a + FIT3SLS = eqnsFit(formulas, data = kmenta, method = "3SLS", + inst = instruments, formula3sls = "GMM") + FIT3SLS + + } # if (SYSTEMFIT) Title: 3SLS Equations Fit Formulas: q ~ p + d q ~ p + f + a Fit Results: N DF SSR MSE RMSE R2 Adj R2 1 20 17 65.7291 3.86642 1.96632 0.754847 0.726005 2 20 16 107.9138 6.74461 2.59704 0.597508 0.522041 The covariance matrix of the residuals used for estimation 1 2 1 3.86642 4.35744 2 4.35744 6.03958 The covariance matrix of the residuals 1 2 1 3.86642 5.00443 2 5.00443 6.74461 The correlations of the residuals 1 2 1 1.00000 0.97999 2 0.97999 1.00000 The determinant of the residual covariance matrix: 1.03320 OLS R-squared value of the system: 0.676177 McElroy's R-squared value for the system: 0.786468 3SLS estimates for 1 (equation 1 ) Model Formula: q ~ p + d Instruments: ~d + f + a Estimate Std. Error t value Pr(>|t|) (Intercept) 94.633304 7.920838 11.947385 0 *** p -0.243557 0.096484 -2.524313 0.021832 * d 0.313992 0.046944 6.688695 4e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.966321 on 17 degrees of freedom Number of observations: 20 Degrees of Freedom: 17 SSR: 65.729088 MSE: 3.866417 Root MSE: 1.966321 Multiple R-Squared: 0.754847 Adjusted R-Squared: 0.726005 3SLS estimates for 2 (equation 2 ) Model Formula: q ~ p + f + a Instruments: ~d + f + a Estimate Std. Error t value Pr(>|t|) (Intercept) 52.197204 11.893372 4.388764 0.000458 *** p 0.228589 0.099673 2.293388 0.035706 * f 0.228158 0.043994 5.186139 9e-05 *** a 0.361138 0.072889 4.954608 0.000143 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 2.597039 on 16 degrees of freedom Number of observations: 20 Degrees of Freedom: 16 SSR: 107.913821 MSE: 6.744614 Root MSE: 2.597039 Multiple R-Squared: 0.597508 Adjusted R-Squared: 0.522041 Description: Wed Jul 13 14:08:22 2005 > > ## SEE ALSO: > # Demo File: xmpEqnsGrunfeld.R > # Estimation of Grunfeld's Model Data with OLS and SUR > > > > cleanEx(); ..nameEx <- "B1-MatrixAddon" > > ### * B1-MatrixAddon > > flush(stderr()); flush(stdout()) > > ### Name: MatrixAddon > ### Title: Matrix Arithmetics and Linear Algebra > ### Aliases: MatrixAddon triang Triang pascal colVec rowVec colIds rowIds > ### colIds<- rowIds<- inv norm rk tr kron mexp vec vech tslag pdl > ### Keywords: math > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > ## SOURCE("fMultivar.B1-MatrixAddon") > > ## Create Pascal Matrix: > xmpMultivar("\nStart: Pascal Matrix > ") > P = pascal(3) > P [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 2 3 [3,] 1 3 6 > # Create lower triangle matrix > L = triang(P) > L [,1] [,2] [,3] [1,] 1 0 0 [2,] 1 2 0 [3,] 1 3 6 > # Extract diagonal part > diag(P) [1] 1 2 6 > > ## Add/Subtract/Multiply/Divide: > xmpMultivar("\nNext: Math Operations > ") > X = P > # Multiply matrix with a constant > 3 * X [,1] [,2] [,3] [1,] 3 3 3 [2,] 3 6 9 [3,] 3 9 18 > # Multiply two matrices elementwise > X * P [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 4 9 [3,] 1 9 36 > # Multiplies rows/columns of a matrix by a vector > X %*% diag(P) [,1] [1,] 9 [2,] 23 [3,] 43 > diag(P) %*% X [,1] [,2] [,3] [1,] 9 23 43 > > ## Operate on Subsets of a Matrix: > xmpMultivar("\nNext: Matrix Subsets > ") > n = 3; i = 2; j = 3 > D = diag(1:3) > # Return the dimension of a matrix > dim(P) [1] 3 3 > # Get the last colum of a matrix > P[, ncol(P)] [1] 1 3 6 > # Delete a column of a matrix > P[, -i] [,1] [,2] [1,] 1 1 [2,] 1 3 [3,] 1 6 > # Permute the columns of a matrix > P[c(3, 1, 2), ] [,1] [,2] [,3] [1,] 1 3 6 [2,] 1 1 1 [3,] 1 2 3 > # Augments matrix horizontally > cbind(P, D) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 1 1 0 0 [2,] 1 2 3 0 2 0 [3,] 1 3 6 0 0 3 > > ## Apply a function to all Elements of a Matrix: > xmpMultivar("\nNext: Operate Element by Element > ") > # Return square root for each element > sqrt(P) [,1] [,2] [,3] [1,] 1 1.000000 1.000000 [2,] 1 1.414214 1.732051 [3,] 1 1.732051 2.449490 > # Exponentiate the matrix elementwise > exp(P) [,1] [,2] [,3] [1,] 2.718282 2.718282 2.718282 [2,] 2.718282 7.389056 20.085537 [3,] 2.718282 20.085537 403.428793 > # Compute the median of each column > apply(P, 2, "median") [1] 1 2 3 > # Test on all elements of a matrix > all( P > 2 ) [1] FALSE > # test on any element in a matrix > any( P > 2 ) [1] TRUE > > ## More Matrix Operations: > xmpMultivar("\nNext: More Matrix Operations > ") > # Return the product of two matrices > P %*% D [,1] [,2] [,3] [1,] 1 2 3 [2,] 1 4 9 [3,] 1 6 18 > # Return the Kronecker Product > P %x% D [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 0 0 1 0 0 1 0 0 [2,] 0 2 0 0 2 0 0 2 0 [3,] 0 0 3 0 0 3 0 0 3 [4,] 1 0 0 2 0 0 3 0 0 [5,] 0 2 0 0 4 0 0 6 0 [6,] 0 0 3 0 0 6 0 0 9 [7,] 1 0 0 3 0 0 6 0 0 [8,] 0 2 0 0 6 0 0 12 0 [9,] 0 0 3 0 0 9 0 0 18 > # Return the transposed matrix > t(P) [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 2 3 [3,] 1 3 6 > # Return the inverse of a matrix > inv(P) [,1] [,2] [,3] [1,] 3 -3 1 [2,] -3 5 -2 [3,] 1 -2 1 > # Return the norm of a matrix > norm(P) [1] 7.872983 > # Return the determinante of a matrix > det(P) [1] 1 > # Return the rank of a matrix > rk(P) [1] 3 > # Return trace of a matrix > tr(P) [1] 9 > # Return the variance matrix > var(P) [,1] [,2] [,3] [1,] 0 0.0 0.000000 [2,] 0 1.0 2.500000 [3,] 0 2.5 6.333333 > # Return the covariance matrix > cov(P) [,1] [,2] [,3] [1,] 0 0.0 0.000000 [2,] 0 1.0 2.500000 [3,] 0 2.5 6.333333 > # Stack a matrix > vec(P) [,1] [1,] 1 [2,] 1 [3,] 1 [4,] 1 [5,] 2 [6,] 3 [7,] 1 [8,] 3 [9,] 6 > # Stack the lower triangle > vech(P) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 1 2 3 6 > > ## Matrix Exponential: > xmpMultivar("\nNext: Linear Algebra > ") > # Test case 1 from Ward (1977) > test1 = t(matrix(c( + 4, 2, 0, + 1, 4, 1, + 1, 1, 4), 3, 3)) > mexp(test1) [,1] [,2] [,3] [1,] 147.8666 183.7651 71.79703 [2,] 127.7811 183.7651 91.88257 [3,] 127.7811 163.6796 111.96811 attr(,"accuracy") [1] 1.774935e-13 attr(,"method") [1] "pade" attr(,"order") [1] 8 > # Results on Power Mac G3 under Mac OS 10.2.8 > # [,1] [,2] [,3] > # [1,] 147.86662244637000 183.76513864636857 71.79703239999643 > # [2,] 127.78108552318250 183.76513864636877 91.88256932318409 > # [3,] 127.78108552318204 163.67960172318047 111.96810624637124 > # -- these agree with ward (1977, p608) > ## Not run: > ##D # A naive alternative to mexp, using spectral decomposition: > ##D mexp2 = function(matrix){ > ##D z = eigen(matrix, sym = FALSE) > ##D Re(z$vectors %*% diag(exp(z$values)) %*% solve(z$vectors)) } > ##D mexp2(test1) > ##D # -- hopelessly inaccurate in all but the first column. > ##D # Test case 4 from Ward (1977) > ##D test4 <- structure(c( > ##D 0, 0, 0, 0, 0, 0, 0, 0, 0, 1e-10, > ##D 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, > ##D 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, > ##D 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, > ##D 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, > ##D 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, > ##D 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, > ##D 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, > ##D 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, > ##D 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), .Dim = c(10, 10)) > ##D attributes(mexp(test4)) > ##D max(abs(mexp(test4) - mexp2(test4))) > ##D #[1] 8.746826694186494e-08 > ##D # -- mexp2 is accurate to 7 d.p., whereas mexp to at least 14 d.p. > ## End(Not run) > > ## More Linear Algebra: > xmpMultivar("\nNext: Linear Algebra > ") > X = P; b = c(1, 2, 3) > # Return the Cholesky factor matrix > chol(X) [,1] [,2] [,3] [1,] 1 1 1 [2,] 0 1 2 [3,] 0 0 1 > # Return eigenvalues and eigenvectors > eigen(X) $values [1] 7.8729833 1.0000000 0.1270167 $vectors [,1] [,2] [,3] [1,] -0.1938227 0.8164966 0.5438438 [2,] -0.4722473 0.4082483 -0.7812271 [3,] -0.8598926 -0.4082483 0.3064605 > # Return the singular value decomposition > svd(X) $d [1] 7.8729833 1.0000000 0.1270167 $u [,1] [,2] [,3] [1,] -0.1938227 0.8164966 0.5438438 [2,] -0.4722473 0.4082483 -0.7812271 [3,] -0.8598926 -0.4082483 0.3064605 $v [,1] [,2] [,3] [1,] -0.1938227 0.8164966 0.5438438 [2,] -0.4722473 0.4082483 -0.7812271 [3,] -0.8598926 -0.4082483 0.3064605 > # Return the condition number of a matrix > kappa(X) [1] 104.4232 > # Return the QR decomposition of a matrix > qr(X) $qr [,1] [,2] [,3] [1,] -1.7320508 -3.4641016 -5.7735027 [2,] 0.5773503 -1.4142136 -3.5355339 [3,] 0.5773503 0.9659258 0.4082483 $rank [1] 3 $qraux [1] 1.5773503 1.2588190 0.4082483 $pivot [1] 1 2 3 attr(,"class") [1] "qr" > # Solve a system of linear equations > # ... use backsolve when the matrix is upper triangular > # ... use forwardsolve when the matrix is lower triangular > solve(X, b) [1] 0 1 0 > backsolve(Triang(X), b) [1] 0.25 0.25 0.50 > solve(Triang(X), b) [1] 0.25 0.25 0.50 > forwardsolve(triang(X), b) [1] 1.00000000 0.50000000 0.08333333 > solve(triang(X), b) [1] 1.00000000 0.50000000 0.08333333 > > > > cleanEx(); ..nameEx <- "B2-MissingValues" > > ### * B2-MissingValues > > flush(stderr()); flush(stdout()) > > ### Name: MissingValues > ### Title: Handling Missing Values > ### Aliases: MissingValues removeNA substituteNA interpNA knnNA > ### Keywords: math > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > ## SOURCE("fMultivar.B2-MissingValues") > > ## Create a Matrix with NAs: > X = matrix(rnorm(100), ncol = 5) > # a single NA inside: > X[3, 5] = NA > # three in a row inside: > X[17, 2:4] = c(NA, NA, NA) > # three in a column inside: > X[13:15, 4] = c(NA, NA, NA) > # two at the right border: > X[11:12, 5] = c(NA, NA) > # one in the lower left corner: > X[20, 1] = NA > print(X) [,1] [,2] [,3] [,4] [,5] [1,] -0.62645381 0.91897737 -0.1645236 2.401617761 -0.5686687 [2,] 0.18364332 0.78213630 -0.2533617 -0.039240003 -0.1351786 [3,] -0.83562861 0.07456498 0.6969634 0.689739362 NA [4,] 1.59528080 -1.98935170 0.5566632 0.028002159 -1.5235668 [5,] 0.32950777 0.61982575 -0.6887557 -0.743273209 0.5939462 [6,] -0.82046838 -0.05612874 -0.7074952 0.188792300 0.3329504 [7,] 0.48742905 -0.15579551 0.3645820 -1.804958629 1.0630998 [8,] 0.73832471 -1.47075238 0.7685329 1.465554862 -0.3041839 [9,] 0.57578135 -0.47815006 -0.1123462 0.153253338 0.3700188 [10,] -0.30538839 0.41794156 0.8811077 2.172611670 0.2670988 [11,] 1.51178117 1.35867955 0.3981059 0.475509529 NA [12,] 0.38984324 -0.10278773 -0.6120264 -0.709946431 NA [13,] -0.62124058 0.38767161 0.3411197 NA 1.1604026 [14,] -2.21469989 -0.05380504 -1.1293631 NA 0.7002136 [15,] 1.12493092 -1.37705956 1.4330237 NA 1.5868335 [16,] -0.04493361 -0.41499456 1.9803999 0.291446236 0.5584864 [17,] -0.01619026 NA NA NA -1.2765922 [18,] 0.94383621 -0.05931340 -1.0441346 0.001105352 -0.5732654 [19,] 0.82122120 1.10002537 0.5697196 0.074341324 -1.2246126 [20,] NA 0.76317575 -0.1350546 -0.589520946 -0.4734006 > > ## Remove rows with NA's > removeNA(X) [,1] [,2] [,3] [,4] [,5] [1,] -0.62645381 0.91897737 -0.1645236 2.401617761 -0.5686687 [2,] 0.18364332 0.78213630 -0.2533617 -0.039240003 -0.1351786 [3,] 1.59528080 -1.98935170 0.5566632 0.028002159 -1.5235668 [4,] 0.32950777 0.61982575 -0.6887557 -0.743273209 0.5939462 [5,] -0.82046838 -0.05612874 -0.7074952 0.188792300 0.3329504 [6,] 0.48742905 -0.15579551 0.3645820 -1.804958629 1.0630998 [7,] 0.73832471 -1.47075238 0.7685329 1.465554862 -0.3041839 [8,] 0.57578135 -0.47815006 -0.1123462 0.153253338 0.3700188 [9,] -0.30538839 0.41794156 0.8811077 2.172611670 0.2670988 [10,] -0.04493361 -0.41499456 1.9803999 0.291446236 0.5584864 [11,] 0.94383621 -0.05931340 -1.0441346 0.001105352 -0.5732654 [12,] 0.82122120 1.10002537 0.5697196 0.074341324 -1.2246126 > # Now we have only 12 lines! > > ## Subsitute NA's by zeros or column mean > substituteNA(X, type = "zeros") [,1] [,2] [,3] [,4] [,5] [1,] -0.62645381 0.91897737 -0.1645236 2.401617761 -0.5686687 [2,] 0.18364332 0.78213630 -0.2533617 -0.039240003 -0.1351786 [3,] -0.83562861 0.07456498 0.6969634 0.689739362 0.0000000 [4,] 1.59528080 -1.98935170 0.5566632 0.028002159 -1.5235668 [5,] 0.32950777 0.61982575 -0.6887557 -0.743273209 0.5939462 [6,] -0.82046838 -0.05612874 -0.7074952 0.188792300 0.3329504 [7,] 0.48742905 -0.15579551 0.3645820 -1.804958629 1.0630998 [8,] 0.73832471 -1.47075238 0.7685329 1.465554862 -0.3041839 [9,] 0.57578135 -0.47815006 -0.1123462 0.153253338 0.3700188 [10,] -0.30538839 0.41794156 0.8811077 2.172611670 0.2670988 [11,] 1.51178117 1.35867955 0.3981059 0.475509529 0.0000000 [12,] 0.38984324 -0.10278773 -0.6120264 -0.709946431 0.0000000 [13,] -0.62124058 0.38767161 0.3411197 0.000000000 1.1604026 [14,] -2.21469989 -0.05380504 -1.1293631 0.000000000 0.7002136 [15,] 1.12493092 -1.37705956 1.4330237 0.000000000 1.5868335 [16,] -0.04493361 -0.41499456 1.9803999 0.291446236 0.5584864 [17,] -0.01619026 0.00000000 0.0000000 0.000000000 -1.2765922 [18,] 0.94383621 -0.05931340 -1.0441346 0.001105352 -0.5732654 [19,] 0.82122120 1.10002537 0.5697196 0.074341324 -1.2246126 [20,] 0.00000000 0.76317575 -0.1350546 -0.589520946 -0.4734006 > substituteNA(X, type = "mean") [,1] [,2] [,3] [,4] [,5] [1,] -0.62645381 0.91897737 -0.1645236 2.401617761 -0.5686687 [2,] 0.18364332 0.78213630 -0.2533617 -0.039240003 -0.1351786 [3,] -0.83562861 0.07456498 0.6969634 0.689739362 0.0325636 [4,] 1.59528080 -1.98935170 0.5566632 0.028002159 -1.5235668 [5,] 0.32950777 0.61982575 -0.6887557 -0.743273209 0.5939462 [6,] -0.82046838 -0.05612874 -0.7074952 0.188792300 0.3329504 [7,] 0.48742905 -0.15579551 0.3645820 -1.804958629 1.0630998 [8,] 0.73832471 -1.47075238 0.7685329 1.465554862 -0.3041839 [9,] 0.57578135 -0.47815006 -0.1123462 0.153253338 0.3700188 [10,] -0.30538839 0.41794156 0.8811077 2.172611670 0.2670988 [11,] 1.51178117 1.35867955 0.3981059 0.475509529 0.0325636 [12,] 0.38984324 -0.10278773 -0.6120264 -0.709946431 0.0325636 [13,] -0.62124058 0.38767161 0.3411197 0.253439667 1.1604026 [14,] -2.21469989 -0.05380504 -1.1293631 0.253439667 0.7002136 [15,] 1.12493092 -1.37705956 1.4330237 0.253439667 1.5868335 [16,] -0.04493361 -0.41499456 1.9803999 0.291446236 0.5584864 [17,] -0.01619026 0.01393998 0.1654293 0.253439667 -1.2765922 [18,] 0.94383621 -0.05931340 -1.0441346 0.001105352 -0.5732654 [19,] 0.82122120 1.10002537 0.5697196 0.074341324 -1.2246126 [20,] 0.16929348 0.76317575 -0.1350546 -0.589520946 -0.4734006 > > ## Interpolate NA's liearily: > interpNA(X, method = "linear") [,1] [,2] [,3] [,4] [,5] [1,] -0.62645381 0.91897737 -0.1645236 2.401617761 -0.5686687 [2,] 0.18364332 0.78213630 -0.2533617 -0.039240003 -0.1351786 [3,] -0.83562861 0.07456498 0.6969634 0.689739362 -0.8293727 [4,] 1.59528080 -1.98935170 0.5566632 0.028002159 -1.5235668 [5,] 0.32950777 0.61982575 -0.6887557 -0.743273209 0.5939462 [6,] -0.82046838 -0.05612874 -0.7074952 0.188792300 0.3329504 [7,] 0.48742905 -0.15579551 0.3645820 -1.804958629 1.0630998 [8,] 0.73832471 -1.47075238 0.7685329 1.465554862 -0.3041839 [9,] 0.57578135 -0.47815006 -0.1123462 0.153253338 0.3700188 [10,] -0.30538839 0.41794156 0.8811077 2.172611670 0.2670988 [11,] 1.51178117 1.35867955 0.3981059 0.475509529 0.5648667 [12,] 0.38984324 -0.10278773 -0.6120264 -0.709946431 0.8626347 [13,] -0.62124058 0.38767161 0.3411197 -0.459598264 1.1604026 [14,] -2.21469989 -0.05380504 -1.1293631 -0.209250098 0.7002136 [15,] 1.12493092 -1.37705956 1.4330237 0.041098069 1.5868335 [16,] -0.04493361 -0.41499456 1.9803999 0.291446236 0.5584864 [17,] -0.01619026 -0.23715398 0.4681326 0.146275794 -1.2765922 [18,] 0.94383621 -0.05931340 -1.0441346 0.001105352 -0.5732654 [19,] 0.82122120 1.10002537 0.5697196 0.074341324 -1.2246126 [20,] NA 0.76317575 -0.1350546 -0.589520946 -0.4734006 > # Note the corner missing value cannot be interpolated! > # Take previous values in a column: > interpNA(X, method = "before") [,1] [,2] [,3] [,4] [,5] [1,] -0.62645381 0.91897737 -0.1645236 2.401617761 -0.5686687 [2,] 0.18364332 0.78213630 -0.2533617 -0.039240003 -0.1351786 [3,] -0.83562861 0.07456498 0.6969634 0.689739362 -0.1351786 [4,] 1.59528080 -1.98935170 0.5566632 0.028002159 -1.5235668 [5,] 0.32950777 0.61982575 -0.6887557 -0.743273209 0.5939462 [6,] -0.82046838 -0.05612874 -0.7074952 0.188792300 0.3329504 [7,] 0.48742905 -0.15579551 0.3645820 -1.804958629 1.0630998 [8,] 0.73832471 -1.47075238 0.7685329 1.465554862 -0.3041839 [9,] 0.57578135 -0.47815006 -0.1123462 0.153253338 0.3700188 [10,] -0.30538839 0.41794156 0.8811077 2.172611670 0.2670988 [11,] 1.51178117 1.35867955 0.3981059 0.475509529 0.2670988 [12,] 0.38984324 -0.10278773 -0.6120264 -0.709946431 0.2670988 [13,] -0.62124058 0.38767161 0.3411197 -0.709946431 1.1604026 [14,] -2.21469989 -0.05380504 -1.1293631 -0.709946431 0.7002136 [15,] 1.12493092 -1.37705956 1.4330237 -0.709946431 1.5868335 [16,] -0.04493361 -0.41499456 1.9803999 0.291446236 0.5584864 [17,] -0.01619026 -0.41499456 1.9803999 0.291446236 -1.2765922 [18,] 0.94383621 -0.05931340 -1.0441346 0.001105352 -0.5732654 [19,] 0.82122120 1.10002537 0.5697196 0.074341324 -1.2246126 [20,] NA 0.76317575 -0.1350546 -0.589520946 -0.4734006 > # Also here, the corner value is excluded > > ## Interpolate using the knn Algorithm: > knnNA(X) [,1] [,2] [,3] [,4] [,5] [1,] -0.62645381 0.91897737 -0.1645236 2.401617761 -0.56866873 [2,] 0.18364332 0.78213630 -0.2533617 -0.039240003 -0.13517862 [3,] -0.83562861 0.07456498 0.6969634 0.689739362 0.30002458 [4,] 1.59528080 -1.98935170 0.5566632 0.028002159 -1.52356680 [5,] 0.32950777 0.61982575 -0.6887557 -0.743273209 0.59394619 [6,] -0.82046838 -0.05612874 -0.7074952 0.188792300 0.33295037 [7,] 0.48742905 -0.15579551 0.3645820 -1.804958629 1.06309984 [8,] 0.73832471 -1.47075238 0.7685329 1.465554862 -0.30418392 [9,] 0.57578135 -0.47815006 -0.1123462 0.153253338 0.37001881 [10,] -0.30538839 0.41794156 0.8811077 2.172611670 0.26709879 [11,] 1.51178117 1.35867955 0.3981059 0.475509529 -0.67989562 [12,] 0.38984324 -0.10278773 -0.6120264 -0.709946431 0.01034039 [13,] -0.62124058 0.38767161 0.3411197 0.183826521 1.16040262 [14,] -2.21469989 -0.05380504 -1.1293631 1.295205030 0.70021365 [15,] 1.12493092 -1.37705956 1.4330237 -0.756756197 1.58683345 [16,] -0.04493361 -0.41499456 1.9803999 0.291446236 0.55848643 [17,] -0.01619026 1.00950137 0.2025980 1.237979542 -1.27659221 [18,] 0.94383621 -0.05931340 -1.0441346 0.001105352 -0.57326541 [19,] 0.82122120 1.10002537 0.5697196 0.074341324 -1.22461261 [20,] 0.25657555 0.76317575 -0.1350546 -0.589520946 -0.47340064 > > > > cleanEx(); ..nameEx <- "C1-TechnicalAnalysis" > > ### * C1-TechnicalAnalysis > > flush(stderr()); flush(stdout()) > > ### Name: TechnicalAnalysis > ### Title: Tools for the Technical Analysis > ### Aliases: TechnicalAnalysis emaTA biasTA medpriceTA typicalpriceTA > ### wcloseTA rocTA oscTA momTA macdTA cdsTA cdoTA vohlTA vorTA fpkTA > ### fpdTA spdTA apdTA wprTA rsiTA SMA EWMA > ### Keywords: math > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > ## SOURCE("fSeries.D1-TechnicalAnalysis") > > # Currently no examples ... > > > > cleanEx(); ..nameEx <- "C2-BenchmarkAnalysis" > > ### * C2-BenchmarkAnalysis > > flush(stderr()); flush(stdout()) > > ### Name: BenchmarkAnalysis > ### Title: Utilities and Benchmark Analysis > ### Aliases: BenchmarkAnalysis ohlcPlot sharpeRatio sterlingRatio > ### maxDrawDown getReturns > ### Keywords: math > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > ## SOURCE("fMultivar.D2-BenchmarkAnalysis") > ## Not run: > ##D ## ohlcPlot - > ##D # Plot OHLC for SP500 > ##D # ohlcPlot(x, ylab = "price", main = instrument) > ##D > ##D ## sharpeRatio - > ##D xmpMultivar("\nStart: Sharpe Ratio > ") > ##D # Sharpe Ratio for DAX and FTSE: > ##D data(EuStockMarkets) > ##D dax = log(EuStockMarkets[, "DAX"]) > ##D ftse = log(EuStockMarkets[, "FTSE"]) > ##D # Ratios: > ##D sharpeRatio(dax) > ##D sharpeRatio(ftse) > ##D > ##D ## maxDrawDown - > ##D xmpMultivar("\nNext: Max-Draw-Downs for the DAX Index >") > ##D par(mfrow = c(1, 1), cex = 0.7) > ##D data(EuStockMarkets) > ##D dax = log(EuStockMarkets[, "DAX"]) > ##D mdd = maxDrawDown(dax) > ##D mdd > ##D # Plot DAX: > ##D plot(dax) > ##D segments(time(dax)[mdd$from], dax[mdd$from], > ##D time(dax)[mdd$to], dax[mdd$from]) > ##D segments(time(dax)[mdd$from], dax[mdd$to], > ##D time(dax)[mdd$to], dax[mdd$to]) > ##D mid = time(dax)[(mdd$from + mdd$to)/2] > ##D arrows(mid, dax[mdd$from], mid, dax[mdd$to], col = 2) > ##D title(main = "DAX: Max Drawdown") > ##D > ##D ## getReturns - > ##D xmpMultivar("\nNext: Returns for the DAX Index >") > ##D par(mfrow = c(1, 1), cex = 0.7) > ##D data(EuStockMarkets) > ##D dax = log(EuStockMarkets[, "DAX"]) > ##D plot(getReturns(dax)) > ##D title(main = "DAX: Returns") > ## End(Not run) > > > > cleanEx(); ..nameEx <- "C3-RollingAnalysis" > > ### * C3-RollingAnalysis > > flush(stderr()); flush(stdout()) > > ### Name: RollingAnalysis > ### Title: Rolling Analysis > ### Aliases: RollingAnalysis rollFun rollMin rollMax rollMean rollVar > ### Keywords: math > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > ## SOURCE("fMultivar.D3-RollingAnalysis") > > ## Rolling Analysis: > ## Not run: > ##D xmpMultivar("\nNext: Rolling Analysis > ") > ##D x = (1:10)^2 > ##D x > ##D trim = c(TRUE, TRUE, FALSE, FALSE) > ##D na.rm = c(TRUE, FALSE, TRUE, FALSE) > ##D for (i in 1:4) > ##D print(rollMin(x, 5, trim[i], na.rm[i])) > ##D for (i in 1:4) > ##D print(rollMax(x, 5, trim[i], na.rm[i])) > ##D for (i in 1:4) > ##D print(rollVar(x, 5, trim[i], unbiased = TRUE, na.rm[i])) > ##D for (i in 1:4) > ##D print(rollVar(x, 5, trim[i], unbiased = FALSE, na.rm[i])) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "X1-MultivarData" > > ### * X1-MultivarData > > flush(stderr()); flush(stdout()) > > ### Name: Series2Data > ### Title: fSeries Data Sets > ### Aliases: Series2Data CobbDouglas logCobbDouglas Greene4Table131 pr45 > ### spc1970 spcindis > ### Keywords: datasets > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > > ## none - > > > > cleanEx(); ..nameEx <- "Z1-MultivarTools" > > ### * Z1-MultivarTools > > flush(stderr()); flush(stdout()) > > ### Name: MultivarTools > ### Title: fMultivar Tools > ### Aliases: MultivarTools xmpMultivar xmpfMultivar > ### Keywords: programming > > ### ** Examples > > ## SOURCE("fBasics.A0-SPlusCompatibility") > > ## Not run: > ##D ## xmpfMultivar - > ##D # Popup the examples menu: > ##D xmpfMultivar() > ## End(Not run) > > > > ### *