<repo_name>cran/FinAna<file_sep>/man/plotsm.Rd

\name{plotsm}
\alias{plotsm}
\title{Plot scatter smooth plots for a data.frame}
\usage{
plotsm(x,dependent,c,l)
}
\arguments{
\item{x}{:a dataframe}
\item{dependent}{:the dependent variable}
\item{c}{:is there dummy variable in the data.frame; c = 0 when there is none; c = 1 when there is}
\item{l}{: number of labeling starts at (default = 1)}
}
\description{
Plotting scatter smooth plots for a data.frame, with name, number and labels.
}
\examples{
# plotsm(JPM-ratios,"price"",0,20)
}

<file_sep>/R/kur.R
###############################################################################################
##### kurtosis
###############################################################################################

#' @name kur
#' @aliases kur
#' @title Calculating kurtosis for numeric data
#' @description Kurtosis
#' @usage kur(x)
#' @param x :a numeric variable
#' @examples #kur(return) for skewness of variable return


kur <- function(x){
  x <- na.omit(x)
  me <- mean(x,na.rm = T)
  med <- median(x, na.rm = T)
  std <- sd(x,na.rm = T)
  kurtosis <- mean((x - me)^4)/(mean((x-me)^2)^2)
  return(kurtosis)
}
<file_sep>/man/get.price.google.Rd

\name{get.price.google}
\alias{get.price.google}
\title{Download financial data from google finance}
\usage{
get.price.google(tkr, bg = "2001-01-01",ed = "today")
}
\arguments{
\item{tkr}{:company ticker, e.g. "BABA","AMZN"}

\item{bg}{:beginning date, e.g."2000-02-29"}

\item{ed}{:ending date, e.g. "today", "2016-11-10"}

}
\description{
Download stock prices for one company or a list of companies from google finance. And furthur application of rate of return function and beta function in the package for more analysis.
}
\examples{
#get.price.google("GOOG")
#get.price.google("GOOG", bg = "2001-01-01",ed = "today")
# the two above are the same
#
# tkr <- c("AAPL", "IBM","YHOO")
# pricelist <- get.price.google(tkr, bg = "2001-01-01",ed = "today")
# aapl <- pricelist[1] # convert to single data.frame
# ibm <- pricelist[2] # convert to single data.frame
# yhoo <- pricelist[3] # convert to single data.frame
}

<file_sep>/R/get.price.google.R
###############################################################################################
##### Download financial data from google
###############################################################################################


get.price.google <- function (tkr, bg = "2001-01-01", ed = "today"){
  n <- length(tkr)
  pricelist <- list()

  bgmth <- month.abb[as.numeric(substring(bg, 6,7))]
  bgday <- substring(bg,9,10)
  bgyear <- substring(bg,1,4)
  if (ed != "today") {
    edmth <- month.abb[as.numeric(substring(ed, 6,7))]
    edday <- substring(ed,9,10)
    edyear <- substring(ed,1,4)
  }
  else {
    ed <- as.character(Sys.Date())
    edmth <- month.abb[as.numeric(substring(ed, 6,7))]
    edday <- substring(ed,9,10)
    edyear <- substring(ed,1,4)
  }
  for (i in 1:n) {
    p <- read.csv(paste0("https://www.google.com/finance/historical?q=", tkr[i], "&output=csv", "&startdate=", bgmth, "+", bgday, "+", bgyear, "&enddate=", edmth, "+", edday, "+", edyear))
    names(p) <- c("date", "open", "high", "low", "close", "volume")
    p$obs <- 1 : length(p$open)
    p <- p[sort(p$obs, decreasing = T),]
    p <- p[,1:6]
    p$obs <- 1 : length(p$open)
    }
    pricelist[[tkr[i]]] = p
  return(pricelist)
}
<file_sep>/R/plotfhs.R
##################################################################################################
### Ploting Function
##################################################################################################

## function to create histogram and scatter plots for data.frame:
## a = 0 for both
## a = 1 for histogram
## a = 2 for scatter plots
## dependent is the column name of the dependent variable
## c = 0 when there is no dummy varibale in the data.frame
## c = 1 when there is dummy varibale in the data.frame
#' @name ploths
#' @aliases ploths
#' @title Plot histograms and scatter plots for a data.frame
#' @description Plotting histograms or scatter plots of your choice for a data.frame. Also the function will name the graphs and number them.The purpose of the function is to save time when plotting graphs for a regression analysis or other usage. The function can plot, name and number the graphs at one step.
#' @usage ploths(x,a,dependent,c,l)
#' @param x :a dataframe
#' @param a :the type of graph you want; a = 1 for histograms; a = 2 for scatter plots; a = 0 for both
#' @param dependent :the dependent variable for scatterplots
#' @param c :is there dummy variable in the dataframe; c = 0 when there is none; c = 1 when there is
#' @param l : number of labeling starts at (default = 1)
#' @examples #ploths(sp500,0,"price",0,20)


ploths <- function(x,a,dependent,c,l = 1){

  yl <- dependent
  dependent <- x[,dependent]
  typeofvar <- sapply(x,class)
  ha <- typeofvar[typeofvar == "numeric" & !names(typeofvar) == dependent]
  if(c == 1){
  dumornot <- typeofvar[typeofvar == "integer"]
  dummyvar <- x[,c(names(dumornot))]
  dummyvar <- dummyvar[!unique(dummyvar) == 0 & !unique(dummyvar) == 1]
  ha <- c(ha,dummyvar)
  }
  x <- x[,c(names(ha))]
  var <- names(x)
  n <- length(var)

  if (a == 1){
    hist(dependent, main = paste("Fig.", paste(l, paste("Histogram of",dependent))), xlab = yl)
    for(i in 1:n){
      hist(x[,i], main = paste("Fig.", paste(i+l, paste("Histogram of",var[i]))), xlab = var[i])
    }
  }else if(a == 2){
    for(i in 1:n){
      plot(x[,i], dependent, main = paste("Fig.", paste(i, paste("Scatter Plot of",var[i]))), xlab = var[i], ylab = yl)
    }
  }else{
    hist(dependent, main = paste("Fig.", paste(l, paste("Histogram of",dependent))), xlab = yl)
    for(i in 1:n){
      hist(x[,i], main = paste("Fig.", paste(i+l, paste("Histogram of",var[i]))), xlab = var[i])}
    for(i in 1:n){
      plot(x[,i], dependent, main = paste("Fig.", paste(i+n+l, paste("Scatter Plot of",var[i]))), xlab = var[i], ylab = yl)
      }
  }
}
<file_sep>/man/getmode.Rd

\name{get.mode}
\alias{get.mode}
\title{Calculating mode for numeric data}
\usage{
get.mode(x)
}
\arguments{
\item{x}{:a numeric variable(vector)}
}
\description{
Calculating mode for numeric data
}
\examples{
# get.mode(return)
}

<file_sep>/R/annu.pv.R
##################################################################################################
### Annuity Present Value
##################################################################################################

#' @name annu.pv
#' @aliases annu.pv
#' @title Calculate present value of annuity
#' @description Calculate present value of an ordinary annuity or an annuity due.
#' @usage annu.pv(pmt,i,n,type = 0)
#' @param pmt :the equal amount of payment of each period
#' @param i :interest rate accoding to the period
#' @param n :number of periods
#' @param type :type = 0 for ordinary annuity, type = 1 for annuity due
#' @examples #annu.pv(100,0.0248,10,0)


annu.pv <- function(pmt,i,n,type = 0){

  if(type == 1){

    pv <- pmt*((1-((1+i)^-n))/i)*(1+i)
  }else{

    pv <- pmt*((1-((1+i)^-n))/i)
  }

  return(pv)
}
<file_sep>/R/betaf.R
###############################################################################################
##### Beta for companies
###############################################################################################

#' @name betaf
#' @aliases betaf
#' @title Calculating beta for a company or a select of companies
#' @description Calculating beta using common method or linear regression(OLS)
#' @usage betaf(x,y,method)
#' @param x :a vector or a data.frame of rate of return of companies
#' @param y :name of the independent variable
#' @param method :method of calculation; method = 1 for a common expression of beta(see detail); method = 2 using linear regression to estimate the beta
#' @examples #betaf(appl,sp500)


betaf <- function(x,y,method){

  dependent <- x[,y]
  typeofvar <- sapply(x,class)
  ha <- typeofvar[typeofvar == "numeric" & !names(typeofvar) == y]
  x <- x[,c(names(ha))]

  if(method == 1){

    n <- dim(x)[2]
    varr <- var(dependent,na.rm = T)
    betare <- as.data.frame(matrix(nrow = n, ncol = 2))
    betare[,1] <- names(x)
    for (i in 1:n){
      covdf <- as.data.frame(cbind(x[,i],dependent))
      covdf <- as.data.frame(na.omit(covdf))
      covv <- cov(covdf[1], covdf[2], use = "na.or.complete")
      betare[i,2] <- covv/varr
    }
  }else{
    n <- dim(x)[2]
    betare <- as.data.frame(matrix(nrow = n, ncol = 2))
    betare[,1] <- names(x)

    for(i in 1:n){
      rrco <- x[,i]
      rdf <- as.data.frame(cbind(rrco, dependent))

      lmFit <- lm(dependent~rrco, rdf, na.omit = na.action)
      betare[i,2] <- lmFit$coefficients[2]
    }

  }
  return(betare)
}
<file_sep>/R/rr.R
###############################################################################################
##### Calculating rate of return of a vector
###############################################################################################

#' @name rr
#' @aliases rr
#' @title Calculating rate of return of a vector
#' @description Calculating the rate of return of a vector for further analysis, including calculating beta of companies, plotting to see the trend of the stock for technical analysis
#' @usage rr(x,n)
#' @param x :a vector of company prices
#' @param n : number of lags
#' @examples #rr(aapl,1)


rr <- function(x, n){

  ratereturn <- c(as.numeric("NA"),(lag(as.ts(x), k = n)/as.ts(x))-1)

  return(ratereturn)
}
<file_sep>/R/desc.R
###############################################################################################
##### Descriptive statistics
###############################################################################################

#' @name desc
#' @aliases desc
#' @title Descriptice statistics of a data.frame
#' @description Calculating the descriptive statistics of a data.frame and exporting in a data.frame
#' @usage desc(x,n)
#' @param x :a data.frame
#' @param n :number of decimal points
#' @examples #desc(sp1500,3) for descriptive statistics of sp1500

desc <- function(x,n){

  typeofvar <- sapply(x,class)
  ha <- names(typeofvar[typeofvar == "numeric" | typeofvar == "integer"])
  x <- x[,c(ha)]

  des <- as.data.frame(matrix(nrow = dim(x)[2], ncol = 11))
  names(des) <- c("name","obs","max","min","mean","median","mode","var","std","skew","kurt")
  des[,1] <- names(x)
  des[,2] <- round(apply(x,2, length), n)
  des[,3] <- round(apply(x,2, max, na.rm = T), n)
  des[,4] <- round(apply(x,2, min, na.rm = T), n)
  des[,5] <- round(apply(x,2,mean, na.rm = T), n)
  des[,6] <- round(apply(x,2,median, na.rm = T), n)
  des[,7] <- round(apply(x,2,get.mode), n)
  des[,8] <- round(apply(x,2,var, na.rm = T), n)
  des[,9] <- round(apply(x,2,sd, na.rm = T), n)
  des[,10] <- round(apply(x,2,sk), n)
  des[,11] <- round(apply(x,2,kur), n)
  return(des)
}
<file_sep>/man/get.price.yahoo.Rd

\name{get.price.yahoo}
\alias{get.price.yahoo}
\title{Download financial data from Yahoo finance}
\usage{
get.price.yahoo(tkr, bg = "first",ed = "today", f = "d")
}
\arguments{
\item{tkr}{:company ticker, e.g. "BABA","AMZN"}

\item{bg}{:beginning date, e.g. "first","2000-02-29"}

\item{ed}{:ending date, e.g. "today", "2016-11-10"}

\item{f}{:frequency, e.g. "d" for daily,"w" for weekly,"m" for monthly}
}
\description{
Download stock prices for one company or a list of companies from Yahoo finance. The function can download daily, weekly and monthly data. And furthur application of rate of return function and beta function in the package for more analysis.
}
\examples{
#get.price.yahoo("GOOG")
#get.price.yahoo("GOOG", bg = "first",ed = "today", f = "d")
# the two above are the same
#
# tkr <- c("AAPL", "IBM","YHOO")
# pricelist <- get.price.yahoo(tkr, bg = "first",ed = "today", f = "m")
# aapl <- pricelist[1] # convert to single data.frame
# ibm <- pricelist[2] # convert to single data.frame
# yhoo <- pricelist[3] # convert to single data.frame
}

<file_sep>/R/plotsm.R
##################################################################################################
### Ploting Function scatter smooth
##################################################################################################


plotsm <- function(x,dependent,c,l = 1){
  x <- na.omit(x)
  yl <- dependent
  dependent <- x[,dependent]
  typeofvar <- sapply(x,class)
  ha <- typeofvar[typeofvar == "numeric"]
  if(c == 1){
    dumornot <- typeofvar[typeofvar == "integer"]
    dummyvar <- x[,c(names(dumornot))]
    dummyvar <- as.data.frame(dummyvar[!unique(dummyvar) == 0 & !unique(dummyvar) == 1])

    if(dim(dummyvar)[1] > 0){
      ha <- c(dumornot,ha)}
  }
  x <- x[,c(names(ha))]
  var <- names(x)
  n <- length(var)
  x <- as.data.frame(na.omit(x))
  for(i in 1:n){
    scatter.smooth(x[,i],dependent, main = paste("Fig.", paste(i+l-1, paste("Scatter Plot of",var[i]))), xlab = var[i],ylab = yl)
  }
}
<file_sep>/R/corm.R
###############################################################################################
##### Correlation Matrix
###############################################################################################

#' @name corm
#' @aliases corm
#' @title Correlation matrix and correlation ranking of a data.frame
#' @description Calculating the descriptive statistics of a data.frame and exporting in a data.frame
#' @usage corm(x,n)
#' @param x :a data.frame
#' @param n :number of decimal points
#' @examples #corm(sp1500,3) for correlation matrix of sp1500


corm <- function(x,n){
  typeofvar <- sapply(x,class)
  ha <- names(typeofvar[typeofvar == "numeric" | typeofvar == "integer" | typeofvar == "double"])
  x <- x[,c(ha)]

  cormatirx <-data.frame(round(cor(x, use = "na.or.complete"), n))
  corrank <- data.frame(sort(cor(x,use = "na.or.complete")[,1],decreasing = T))
  return(cormatirx)
  return(corrank)
}
<file_sep>/R/get.price.yahoo.R
###############################################################################################
##### Download financial data from yahoo
###############################################################################################


get.price.yahoo <- function (tkr, bg = "first", ed = "today", f = "d"){
  num <- length(tkr)
  pricelist <- list()
  if (bg == "first" & ed == "today") {
    for (i in 1:num) {
      p <- read.csv(paste0("http://ichart.finance.yahoo.com/table.csv?s=", tkr[i]))
      p <- p[order(p$Date),]
      names(p) <- c("date", "open", "high", "low", "close", "volume", "adjusted")
      p$obs <- c(1:length(p[,1]))
      pricelist[[tkr[i]]] <- p
    }
  }else{
    bgmth <- as.character(as.numeric(substring(bg,6,7))-1)
    bgday <- substring(bg,9,10)
    bgyear <- substring(bg,1,4)
    if (ed != "today") {
      edmth <- as.character(as.numeric(substring(ed,6,7))-1)
      edday <- substring(ed,9,10)
      edyear <- substring(ed,1,4)
    }else{
      ed <- as.character(substring(Sys.time(),1,10))
      edmth <- substring(ed,6,7)
      edday <- substring(ed,9,10)
      edyear <- substring(ed,1,4)
    }
    for (i in 1:num) {
      p <- read.csv(paste0("http://ichart.finance.yahoo.com/table.csv?s=", tkr[i], "&a=", bgmth, "&b=", bgday, "&c=", bgyear, "&d=", edmth, "&e=", edday, "&f=", edyear, "&g=", f))
      p <- p[order(p$Date),]
      names(p) <- c("date", "open", "high", "low", "close", "volume", "adjusted")
      p$obs <- c(1:length(p[,1]))
      pricelist[[tkr[i]]] <- p
    }
  }
  return(pricelist)
}
<file_sep>/R/ploth.R
##################################################################################################
### Ploting Function
##################################################################################################

## function to create histogram and scatter plots for data.frame:
## c = 0 when there is no dummy varibale in the data.frame
## c = 1 when there is dummy varibale in the data.frame
#' @name ploth
#' @aliases ploth
#' @title Plot histograms for a data.frame
#' @description Plotting histograms for a data.frame. Also the function will name the graphs and number the graphs.
#' @usage ploth(x,c,l)
#' @param x :a dataframe
#' @param c :is there dummy variable in the data.frame; c = 0 when there is none; c = 1 when there is
#' @param l : number of labeling starts at (default = 1)
#' @examples #ploth(sp500,0,20) for histograms of sp500 which does not has dummy variables


ploth <- function(x,c,l = 1){
  typeofvar <- sapply(x,class)
  ha <- typeofvar[typeofvar == "numeric"]
  if(c == 1){
    dumornot <- typeofvar[typeofvar == "integer"]
    dummyvar <- x[,c(names(dumornot))]
    dummyvar <- as.data.frame(dummyvar[!unique(dummyvar) == 0 & !unique(dummyvar) == 1])
    if(dim(dummyvar)[1] > 0){
    ha <- c(dumornot,ha)}
    }
  x <- x[,c(names(ha))]
  var <- names(x)
  n <- length(var)
    for(i in 1:n){
      hist(x[,i], main = paste("Fig.", paste(l+i-1, paste("Histogram of",var[i]))), xlab = var[i])
    }
}
<file_sep>/R/annu.pv.df.R
##################################################################################################
### Deffered Annuity Present Value
##################################################################################################

#' @name annu.pv.df
#' @aliases annu.pv.df
#' @title Calculate present value of annuity
#' @description Calculate present value of an ordinary annuity or an annuity due.
#' @usage annu.pv.df(pmt,i,n,k)
#' @param pmt :the equal amount of payment of each period
#' @param i :interest rate accoding to the period
#' @param n :number of periods
#' @param k :number of periods deffered
#' @examples #annu.pv(100,0.0248,10,4,0)


annu.pv.df <- function(pmt,i,n,k){

    pv <- pmt*((1-((1+i)^-n))/i)*((1+i)^-k)
    round(pv,2)

  return(pv)
}
<file_sep>/R/bond.price.R
##################################################################################################
### Plain Vanilla Bond
##################################################################################################

#' @name bond.price
#' @aliases bond.price
#' @title Calculate the plain vanilla bond price
#' @description Calculate the plain vanilla bond price
#' @usage bond.price(par,c,n,yield,m)
#' @param par :the face value of the bond
#' @param c :the annual coupon rate of the bond
#' @param n :number of years
#' @param yield :the annual yield to maturity of a bond
#' @param m :couponding period in a year
#' @examples #bond.price(1000,0.03,10,0.0248,2)


bond.price <- function(par,c,n,yield,m){

  pmt <- c*par/m
  p <- pmt*((1-((1+yield/m)^(-m*n)))/(yield/2))+(par/((1+yield/m)^(m*n)))

  return(p)

}
