**R-Chart**, and kindly contributed to R-bloggers)

Mortgage rates have been at historic lows recently. The rates are posted various places online along with simple mortgage calculators. Such calculators illustrate the payment schedule for a mortgage based upon selected terms. But with less than a dozen lines of R code, you can do a far more sophisticated analysis.

**Mortgage Calculation Function**

Rather than reinvent the wheel, you can work with this nice R function by Thomas Girke (Associate Professor of Bioinformatics over at UC Riverside). At the R prompt, you can grab it from its home online by calling source:

source(“http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/My_R_Scripts/mortgage.R”)

This loads the function and outputs a helpful description of the function:

The monthly mortgage payments and amortization rates can be calculted with the mortgage() function like this:

mortgage(P=500000, I=6, L=30, amort=T, plotData=T)

P = principal (loan amount)

I = annual interest rate

L = length of the loan in years

**Instant R Graphical User Interfaces**

Rather than simply calling the function directly, you can quickly construct a GUI input widget using the fgui library.

library(fgui)

gui(mortgage)

With this trivial invocation, a window pops up.

Not terribly fancy, but about the simplest way you will ever be able to construct a GUI! In this case a mortgage amount of $90,000 for 10 years at 3.75% is illustrated.

After entering these values, click OK to actually call the function. This results in a good deal of interesting output. Close the pop up window and look at the R Console:

Monthly payment: $900.5512 (stored in monthPay)

Total cost: $108066.1

As indicated in this message, an R object named monthPay contains the amount of the monthly payment and can be used in subsequent R commands and calculations. You also are greeted with a graph illustrating annual interest and payments as a stacked bar chart.

Plenty of useful information! But that’s not all…**Beyond the Basics**

You might have noticed a number of messages regarding data stored in R objects. This is where the power of R exceeds that of any standard mortgage calculator. These objects can serve as input to other calculations or charting operations.

The aDFmonth object contains amortization data for each month, while aDFyear contains annual information. In the following example, a new data frame is created from the monthly data that does not include the amortization information and plot it using ggplot2. (The amortization data is a significantly different scale and better viewed independently).

library(ggplot2)

DF=melt(aDFmonth[-1], id.vars=’Year’)

**leave a comment**for the author, please follow the link and comment on their blog:

**R-Chart**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...