Sweave (Latex interface with R)
By tholscla on Tue, 11/12/2013 - 12:39I have found Sweave is great for at least two things: generating automated reports and writing R tutorials with code.
I have found Sweave is great for at least two things: generating automated reports and writing R tutorials with code.
Here are 4 quick ways to plot 2D distributions. See here for more color options.
library(MASS) #kde2d() and mvrnorm()
VV=matrix(c(10,8,8,10),2,2)
coef=mvrnorm(10000,c(2,10),VV)
par(mfrow=c(2,2), mar=c(4,4,1,1))
### 1
plot(coef[,1],coef[,2])
### 2
contour(kde2d(coef[,1],coef[,2]))
### 3
persp(kde2d(coef[,1],coef[,2]))
### 4
image(kde2d(coef[,1],coef[,2], n = 100))
box()
rainbow(), heat.colors(), topo.colors(), terrain.colors() [tim.colors() library(fields)] can all be a bit overwhelming use of color. colorRampPalette() can be used for more control over the color mixture. densCols() gives color by density. Here I give a function that makes monochromatic color. See the rows of the figure for some of the different monochromatic color bands that are possible.
This is a set of functions that is similar to the lm() function in R for ordinary least squares (OLS). Except this is code for Bayesian regression with reference priors on the coefficients and the variance parameters. Here is a .pdf file with the full explanation and derivation of the posterior full conditionals for the parameters.
This calculates the mode of a vector (like mean() and median()). Finds the most common element in a vector.
mode=function(vector_name)
{ as.numeric(names(sort(table(vector_name),decreasing=TRUE))[1]) }
This function will sort a dataframe by the column number specified.
First cbind() your variables together and then sort by one column.
ordering=function(dataset, col.num)
{ index=order(dataset[,col.num])
hold=0
dataset=dataset[index,]
dataset
}
### Example:
# turtles=cbind(w,f,y)
# ordering(turtles, 3)
Wraps R.rgamma for correct input and output
double rgammadouble(int a, double b, double c)
{ Rcpp::NumericVector x = rgamma(a,b,1/c);
return x(0);
}
Wraps R.rbinom for correct input and output
double rbinomint(int a, int b, double c)
{ Rcpp::NumericVector x = rbinom(a,b,c);
return x(0);
}
Wraps R.rnorm for correct input and output.
double rnormdouble(int a, double b, double c)
{ Rcpp::NumericVector x = rnorm(a,b,c);
return x(0);
}
Wraps R.runif for correct input and output.
double runifdouble()
{ Rcpp::NumericVector x = runif(1);
return x(0);
}