# Trying to Generalize a Function in R

9 messages
Open this post in threaded view
|

## Trying to Generalize a Function in R

 I wrote the following function: # This method gets historical stock data for the stock Avalon Bay whose symbol is AVB. getReturns <- function(norm = FALSE) {      library(quantmod)      getSymbols("AVB", src = "yahoo", from = start, to = end)      length = length(  AVB\$AVB.Close )      close = as.numeric( AVB\$AVB.Close )      cat( "length = ", length(close ), "\n" )      for( i in 1:length-1 )          diff[i] = ((close[i+1] - close[i]) ) / close[i]      u = mean(diff)      stdDev = sd(diff)      cat( "stdDev = ", stdDev, "\n" )      if ( norm == TRUE ) {          diff = (diff - u)          diff = diff / stdDev      }      return (diff) } I would like to generalize it to work for any stock by passing in the stock symbol. So the header for the function would be: getReturns <- function(symbol, norm = FALSE) Now how do I update this line:      length = length(  AVB\$AVB.Close ) This statement will not work:      length = length(  symbol\$AVB.Close ) because the name that holds the closing price is a function of the stock symbol. Thanks, Bob ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Trying to Generalize a Function in R

 If I understand it correctly, the function getSymbols creates a variable with the name being the stock symbol. Then use the function get(symbol) to retrieve the value of the variable whose name is contained in the character string `symbol'. Assign that to a variable (e.g. AVB). You may also have to modify the names of the components you retrieve from the list AVB. For that, you can use AVB[["AVB.Close"]] instead of AVB\$AVB.Close. You can them use something like AVB[[paste0(symbol, ".Close"]] to generalize the retrieval of list components. HTH, Peter On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> wrote: > > > I wrote the following function: > > # This method gets historical stock data for the stock Avalon Bay whose > symbol is AVB. > getReturns <- function(norm = FALSE) > { >      library(quantmod) > >      getSymbols("AVB", src = "yahoo", from = start, to = end) >      length = length(  AVB\$AVB.Close ) >      close = as.numeric( AVB\$AVB.Close ) >      cat( "length = ", length(close ), "\n" ) >      for( i in 1:length-1 ) >          diff[i] = ((close[i+1] - close[i]) ) / close[i] >      u = mean(diff) >      stdDev = sd(diff) >      cat( "stdDev = ", stdDev, "\n" ) > >      if ( norm == TRUE ) { >          diff = (diff - u) >          diff = diff / stdDev >      } >      return (diff) > } > > I would like to generalize it to work for any stock by passing in the > stock symbol. So the header for the > function would be: > > getReturns <- function(symbol, norm = FALSE) > > Now how do I update this line: >      length = length(  AVB\$AVB.Close ) > This statement will not work: >      length = length(  symbol\$AVB.Close ) > because the name that holds the closing price is a function of the stock > symbol. > > Thanks, > Bob > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Trying to Generalize a Function in R

 Peter, Thanks for the response. I tired the following command:      AVB[["AVB.Close"]] and I got:      Error in AVB[["AVB.Close"]] : subscript out of bounds Are you assuming that AVB is a data frame? I do not think AVB is a data frame. Is there a way for me to check? Thanks, Bob On 8/9/2018 3:46 PM, Peter Langfelder wrote: > If I understand it correctly, the function getSymbols creates a > variable with the name being the stock symbol. Then use the function > get(symbol) to retrieve the value of the variable whose name is > contained in the character string `symbol'. Assign that to a variable > (e.g. AVB). You may also have to modify the names of the components > you retrieve from the list AVB. For that, you can use > AVB[["AVB.Close"]] instead of AVB\$AVB.Close. You can them use > something like AVB[[paste0(symbol, ".Close"]] to generalize the > retrieval of list components. > > HTH, > > Peter > On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> wrote: >> >> I wrote the following function: >> >> # This method gets historical stock data for the stock Avalon Bay whose >> symbol is AVB. >> getReturns <- function(norm = FALSE) >> { >>       library(quantmod) >> >>       getSymbols("AVB", src = "yahoo", from = start, to = end) >>       length = length(  AVB\$AVB.Close ) >>       close = as.numeric( AVB\$AVB.Close ) >>       cat( "length = ", length(close ), "\n" ) >>       for( i in 1:length-1 ) >>           diff[i] = ((close[i+1] - close[i]) ) / close[i] >>       u = mean(diff) >>       stdDev = sd(diff) >>       cat( "stdDev = ", stdDev, "\n" ) >> >>       if ( norm == TRUE ) { >>           diff = (diff - u) >>           diff = diff / stdDev >>       } >>       return (diff) >> } >> >> I would like to generalize it to work for any stock by passing in the >> stock symbol. So the header for the >> function would be: >> >> getReturns <- function(symbol, norm = FALSE) >> >> Now how do I update this line: >>       length = length(  AVB\$AVB.Close ) >> This statement will not work: >>       length = length(  symbol\$AVB.Close ) >> because the name that holds the closing price is a function of the stock >> symbol. >> >> Thanks, >> Bob >> >> ______________________________________________ >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >> 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Trying to Generalize a Function in R

Open this post in threaded view
|

## Re: Trying to Generalize a Function in R

 In reply to this post by rsherry8 Well, your function uses AVB\$AVB.Close, so I assumed AVB is a list (a data frame can be thought of as a special list). What do you get when you type class(AVB)? Peter On Thu, Aug 9, 2018 at 2:24 PM rsherry8 <[hidden email]> wrote: > > Peter, > > Thanks for the response. I tired the following command: >      AVB[["AVB.Close"]] > and I got: >      Error in AVB[["AVB.Close"]] : subscript out of bounds > Are you assuming that AVB is a data frame? I do not think AVB is a data > frame. Is there a way > for me to check? > Thanks, > Bob > > On 8/9/2018 3:46 PM, Peter Langfelder wrote: > > If I understand it correctly, the function getSymbols creates a > > variable with the name being the stock symbol. Then use the function > > get(symbol) to retrieve the value of the variable whose name is > > contained in the character string `symbol'. Assign that to a variable > > (e.g. AVB). You may also have to modify the names of the components > > you retrieve from the list AVB. For that, you can use > > AVB[["AVB.Close"]] instead of AVB\$AVB.Close. You can them use > > something like AVB[[paste0(symbol, ".Close"]] to generalize the > > retrieval of list components. > > > > HTH, > > > > Peter > > On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> wrote: > >> > >> I wrote the following function: > >> > >> # This method gets historical stock data for the stock Avalon Bay whose > >> symbol is AVB. > >> getReturns <- function(norm = FALSE) > >> { > >>       library(quantmod) > >> > >>       getSymbols("AVB", src = "yahoo", from = start, to = end) > >>       length = length(  AVB\$AVB.Close ) > >>       close = as.numeric( AVB\$AVB.Close ) > >>       cat( "length = ", length(close ), "\n" ) > >>       for( i in 1:length-1 ) > >>           diff[i] = ((close[i+1] - close[i]) ) / close[i] > >>       u = mean(diff) > >>       stdDev = sd(diff) > >>       cat( "stdDev = ", stdDev, "\n" ) > >> > >>       if ( norm == TRUE ) { > >>           diff = (diff - u) > >>           diff = diff / stdDev > >>       } > >>       return (diff) > >> } > >> > >> I would like to generalize it to work for any stock by passing in the > >> stock symbol. So the header for the > >> function would be: > >> > >> getReturns <- function(symbol, norm = FALSE) > >> > >> Now how do I update this line: > >>       length = length(  AVB\$AVB.Close ) > >> This statement will not work: > >>       length = length(  symbol\$AVB.Close ) > >> because the name that holds the closing price is a function of the stock > >> symbol. > >> > >> Thanks, > >> Bob > >> > >> ______________________________________________ > >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see > >> 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. > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Trying to Generalize a Function in R

 Peter, Here is the R command and its output that you requested:  > class(AVB) [1] "xts" "zoo" Bob On 8/9/2018 5:29 PM, Peter Langfelder wrote: > Well, your function uses AVB\$AVB.Close, so I assumed AVB is a list (a > data frame can be thought of as a special list). What do you get when > you type class(AVB)? > > Peter > On Thu, Aug 9, 2018 at 2:24 PM rsherry8 <[hidden email]> wrote: >> Peter, >> >> Thanks for the response. I tired the following command: >>       AVB[["AVB.Close"]] >> and I got: >>       Error in AVB[["AVB.Close"]] : subscript out of bounds >> Are you assuming that AVB is a data frame? I do not think AVB is a data >> frame. Is there a way >> for me to check? >> Thanks, >> Bob >> >> On 8/9/2018 3:46 PM, Peter Langfelder wrote: >>> If I understand it correctly, the function getSymbols creates a >>> variable with the name being the stock symbol. Then use the function >>> get(symbol) to retrieve the value of the variable whose name is >>> contained in the character string `symbol'. Assign that to a variable >>> (e.g. AVB). You may also have to modify the names of the components >>> you retrieve from the list AVB. For that, you can use >>> AVB[["AVB.Close"]] instead of AVB\$AVB.Close. You can them use >>> something like AVB[[paste0(symbol, ".Close"]] to generalize the >>> retrieval of list components. >>> >>> HTH, >>> >>> Peter >>> On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> wrote: >>>> I wrote the following function: >>>> >>>> # This method gets historical stock data for the stock Avalon Bay whose >>>> symbol is AVB. >>>> getReturns <- function(norm = FALSE) >>>> { >>>>        library(quantmod) >>>> >>>>        getSymbols("AVB", src = "yahoo", from = start, to = end) >>>>        length = length(  AVB\$AVB.Close ) >>>>        close = as.numeric( AVB\$AVB.Close ) >>>>        cat( "length = ", length(close ), "\n" ) >>>>        for( i in 1:length-1 ) >>>>            diff[i] = ((close[i+1] - close[i]) ) / close[i] >>>>        u = mean(diff) >>>>        stdDev = sd(diff) >>>>        cat( "stdDev = ", stdDev, "\n" ) >>>> >>>>        if ( norm == TRUE ) { >>>>            diff = (diff - u) >>>>            diff = diff / stdDev >>>>        } >>>>        return (diff) >>>> } >>>> >>>> I would like to generalize it to work for any stock by passing in the >>>> stock symbol. So the header for the >>>> function would be: >>>> >>>> getReturns <- function(symbol, norm = FALSE) >>>> >>>> Now how do I update this line: >>>>        length = length(  AVB\$AVB.Close ) >>>> This statement will not work: >>>>        length = length(  symbol\$AVB.Close ) >>>> because the name that holds the closing price is a function of the stock >>>> symbol. >>>> >>>> Thanks, >>>> Bob >>>> >>>> ______________________________________________ >>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >>>> 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|