# R

## table() function

Retrieving information from the table function:

`x=c("river", "stream","stream","stream","river","river","river","stream","stream","stream", "flood")y= table(x)names(y)as.vector(y)sort(y,decreasing=TRUE)`

## Weighted Regression

Here is a quick example of how to recreate weighted regression without needing the weights=... part of lm().  It also shows how to set up your own intercept in R.

`set.seed(50)x0=rep(1,100)         #specify the intercept because they multiply by the weightsx=round(runif(100),2)*100w=(runif(100,1,10))^2y=round(3*x+rnorm(100,0,10),2)plot(x,y)X=cbind(x0,x)`

## NHMM Package

Download R from CRAN.  Open R and at the prompt start installing packages.  install.packages("Rcpp")  install.packages("msm")  install.packages("MCMCpack")  install.packages("BayesLogit").  Place the NHMM folder (link below) in the same directory where the new libraries were just installed.

Open R and at the prompt use the library(NHMM). It contains the modeling functions: NHMM and HMM and NHMM_MVN, and output functions: OBIC, Oz, Oemparams, OWcoef, OXcoef, and OQQ. For the help files just put ?NHMM or ?OBIC

## 3 Birthdays

This code computes the probability that three (and possibly more) people in a group have the same birthday.

## Progress bar

I usually use a print() statement within for loops but this progress bar seems better.  I wish packages had this included.

`Q = 10000pb = txtProgressBar(min = 0, max = Q, style = 3)for(i in 1:Q){  Sys.sleep(0.01)  setTxtProgressBar(pb, i)}close(pb)`

## Installing Rcpp on Windows (8.1)

Rcpp works well with Unix/Linux.  Setting it up on Windows takes some work. Use the standard settings on all of these installs. (updated 2/2014)

## Inverse Gamma distribution

dinvgamma=function(x,a,b){exp(a*log(b) - lgamma(a) - (a+1)*log(x) - b*1.0/x)}

rinvgamma=function(n,a,b){1/rgamma(n,a,b)}  #at one point the MCMCpack library has this function with different parameters for Windows and Unix

## Converting Netcdf files to R

Occasionally, I need to open .nc files. The names command does not work quite as I expected. Instead there is a names command to see all of the variables in the dataset.

`library(ncdf)data = open.ncdf(filename.nc)print(data)  #gives an overview of some of the contents of the netcdf filenames(data\$var)   #gives the actual variable namesvar1_R=get.var.ncdf(data, "var1")   ## assume var1 is one of the variables in the dataset`

## Converting Matlab files to R

On occassion I have need to open Matlab files, usually cotaining data.  library(R.matlab) and library(R.utils) are needed.  The quick command is data=readMat(filename.mat).

## 95% probability intervals (PI)

I typicallly work with models and MCMC algorithms with many unknown parameters.  At the end of the algorithm, all I really want is the mean and 95% PI.  Here is some code that only saves the extreme 5%  (2.5%,97.5%) of the posterior draws for a parameter.  This cuts down on memory space if there are a lot of unknown parameters.