calculate portfolio of varying size based on past returns

#1
Dear all!

First of all I would like to note that I am not trying to get someone to do the work for me. This is intended to be a feasibility question (i.e. I would like to know if it can be done and if so, where can I find more information on how to achieve it).

So, here's what I would like to achieve (btw. I am using RStudio):
I would like to calculate returns of different portfolios formed based on past returns.

And here are my issues:
  • Shares are not included for the entire time frame, therefore there are a lot of #N/A values. If there's an error in one field, R is supposed to ignore the entire column.
  • Next, the amount of shares included varies in time, so I would like to know if it is possible to tell R to count the amount included and form equally sized groups/portfolios (or as equal as possible since for example 158 can not be divided by 5 or 10).
  • How can I tell R to filter (ascending or descending) for example row D based on (row A + row B)?

I hardly doubt this can be achieved, but the alternative would be doing it manually in excel and that can hardly be the way it's supposed to be done...:/

I would be very glad if anyone could guide/help me!

Many thanks in advance!
 
#2
Suppose you have a matrix where the top is data from long long ago (e.g. 1988) and the bottom is recent data (e.g. 2012). So the early dates are at the top.

Let this matrix be called stockdata. Let the rownames(stockdata) be the dates. Let the colnames(stockdata) be the names of the firms.

Let each element be the log return of stock $i$ at time $t$.

Then the function to calculate the unweighted portfolio time series:

Code:
unweighted <- function(stockdata){
	out <- sapply(seq(nrow(stockdata)),function(i) mean(stockdata[i,],na.rm=TRUE))
	out[which(out==0)] <- NA
	out[which(abs(out)==Inf)] <- NA
	return(out)
}
unweightedportfolio <- unweighted(stockdata)
What topic is your thesis on?

I have no idea about your 3rd dot-point; couldn't understand what you said.

If stockdata is a matrix of PRICES and not log returns, then you can calculate the returns matrix by going:

Code:
stockdata <- log(stockdata[2:nrow(stockdata),]/stockdata[1:(nrow(stockdata)-1),])
Assuming that your data is ordered as I said in this post's preamble.