Skip to content

Instantly share code, notes, and snippets.

if (na.omit)
x <- x[!is.na(x)]
n <- length(x)
m <- mean(x)
s <- sd(x)
t <- (m-1)/(s/n^0.5)
med <- median(x)
skew <- sum((x-m)^3/s^3)/n
ses <- ((6*n*(n-1))/((n-1)*(n+1)*(n+3)))^0.5
kurt <- sum((x-m)^4/s^4)/n - 3
@RobHayward
RobHayward / percentagechange.R
Last active June 14, 2017 06:59
Percentage change in a data frame
# create the lagged variable
da$GDPl <- c(rep(NA, 1), da$GDP)[1:length(da$GDP)]
# calculate the percentage
da$GDPr <- (da$GDP - da$GDPl)/da$GDPl
# or with data from latest to oldest
da$SPYl <- c(da$SPY[2:length(da$SPY)], rep(NA, 1))
da$SPYr <- (da$SPY - da$SPYl)/da$SPYl
@RobHayward
RobHayward / labelmosaic.R
Last active December 7, 2016 09:02
Put labels in the mosaic.
mosaicplot(x2, main = "Blue and Green Cabs correct and incorrect identification",
xlab = "Cab colour", ylab = "Identification", color = colours)
# set the text position as a percentage of the plot space for x and y.
text(x = 0.1, y = 0.5, label = 0.12)
text(x = 0.1, y = 0.9, label = 0.03)
text(x = 0.6, y = 0.5, label = 0.68)
text(x = 0.6, y = 0.9, label = 0.17)
text(x = 15, y = 16, labels = "12.5", pos = 4)
@RobHayward
RobHayward / plotLab.R
Created November 21, 2016 15:58
Keep the labels of the plot clear so that own labels can be used.
# xaxt will determine the x axis. = "n" leave clear.
plot(form, type='o', main="Grades Relative to Average (av=1)", xaxt = 'n')
axis(1,1:6,labels=c("AS", "A","B","C","D","FU"))
@RobHayward
RobHayward / return.R
Created October 21, 2016 08:40
Create returns from raw data
#http://stackoverflow.com/questions/14614710/calculate-percentage-change-in-an-r-data-frame
SPY.R <- diff(da$SPY)/da$SPY[-length(da$SPY)]
TLT.R <- diff(da$TLT)/da$TLT[-length(da$TLT)]
@RobHayward
RobHayward / WrapTikzText.tex
Created October 17, 2016 13:57
Wrap text in node and wrap general text in tikz. Link to further suggestions.
\begin{tikzpicture}
%\draw[very thin, color = gray](0, 0) grid (12, 6);
\tikzstyle{block} = [draw, rectangle, text width = 5em,
text centered, minimum height = 6mm, node distance = 5em];
\tikzstyle{line} = [draw, -stealth, thick]
\node [block] at (1.5, 4) (one) {\scriptsize True 'market' \\ (unobserved)};
\node [block] at (5, 4) (two) {\scriptsize Passive investable \\ benchmark};
\node [block] at (8.5, 4) (three) {\scriptsize Rebalanced \\ benchmark};
\node [block] at (8.5, 2) (four) {\scriptsize Factor \\ tilts};
\node [block] at (8.5, 0) (five) {\scriptsize Active \\ Portfolio};
@RobHayward
RobHayward / Posgraphic.tex
Last active October 11, 2016 10:07
Position graphics in beamer slide
\usepackage[absolute, overlay]{textpos}
\begin{frame}{Price comparison}
\begin{textblock*}{5cm}(1cm, 1.5cm) % (block width) (coords)
\includegraphics[width = 5cm]{./Pictures/ebaylogo}
\end{textblock*}
\begin{textblock*}{5cm}(6cm, 4cm)
\includegraphics[width = 5cm]{./Pictures/gocompare}
\end{textblock*}
\end{frame}
@RobHayward
RobHayward / Dist.Rnw
Created October 5, 2016 08:32
Slide with normal distribution together with a skew and fat-tailed. Taken from Stack Exchange. Not perfect but...
\begin{frame}{Normal distribution}
<<norm, fig.height=4.6, echo=FALSE, warning=FALSE>>=
x <- seq(-3, 3, 0.1)
fs = function(x, epsilon, delta){
dnorm(sinh(delta*asinh(x) - epsilon)) * delta*cosh(delta*asinh(x) -
epsilon)/sqrt(1 + x^2)
}
plot(x, fs(x, 0, 1), type = 'l', col = 'darkgreen', main = 'Normal Distribution',
xlab = 'Return', ylab = 'frequency', ylim = c(0, 0.6))
# http://stats.stackexchange.com/questions/43482/transformation-to-increase-kurtosis-and-skewness-of-normal-r-v
# This is not mine. I forget whoes it is. Sorry. By all means let me know and I will add it
acf2=function(series,max.lag=NULL){
num=length(series)
if (is.null(max.lag)) max.lag=ceiling(10+sqrt(num))
if (max.lag > (num-1)) stop("Number of lags exceeds number of observations")
ACF=acf(series, max.lag, plot=FALSE)$acf[-1]
PACF=pacf(series, max.lag, plot=FALSE)$acf
LAG=1:max.lag/frequency(series)
minA=min(ACF)
maxA=max(ACF)
da <- read.csv("file", stringsAsFactors = FALSE)
# Reduce the questions to one word
Questions <- c("Overall", "Organised", "Informed", "Changes", "Contract", "Teaching", "Stimulated", "Assessment",
"Feedback-time", "Feedback-use", "Resources")
#Wrap two words labels to fit on chart without overlap
Level <- c(paste("Strongly", "Agree", sep = "\n"), "Agree", "Unsure", "Disagree", paste("Strongly", "Disagree", sep = "\n"))
barplot(da[1,])
str(da)
head(da, n = 20)
#-------------------------------------