diff --git a/.DS_Store b/.DS_Store index 5008ddf..3b98d1b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..28f84d1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: r +sudo: required + +r_github_packages: + - ./travis-tool.sh github_package jimhester/covr + +after_success: + - Rscript -e 'library(covr);coveralls()' + \ No newline at end of file diff --git a/CompPack/CompPack.Rproj b/CompPack/CompPack.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/CompPack/CompPack.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/CompPack/debug.log b/CompPack/debug.log new file mode 100644 index 0000000..e69de29 diff --git a/DESCRIPTION b/DESCRIPTION index 7d36cb0..23c17e3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,28 +3,52 @@ Type: Package Title: Collection of Computational Statistic Tools Version: 1.0 Date: 2013-04-10 -Author: Tyler Hunt +Author: Tyler Hunt , Katie Dodds , + Ernest Chan , Damon McCafferty + Maintainer: Tyler Hunt Description: The functions in this package were developed in a computational statistics course taught at the University of Utah. License: MIT Collate: + 'anneal.R' + 'around.R' + 'cvscore.R' + 'cvscore1.R' + 'cvscore2.R' + 'cvscore3.R' + 'cvscore4.R' + 'cvscore5.R' + 'd1f.R' + 'd2f.R' 'em.mixnorm.R' + 'f.models.R' 'gold.sect.R' 'grid.sect.R' + 'jackknife.R' + 'log.lik.R' 'maxbound.R' 'mc.R' + 'mixlike.R' 'mixnorm.R' + 'nearest.R' + 'nearest.loc.R' 'newton.raph.R' + 'onedfunction.R' 'plot.mc.R' + 'pois.int.R' + 'pois.proc.R' + 'pois.rate.R' + 'polar.rnorm.R' 'rand.exp.R' 'rand.mvnorm.R' 'rand.norm.R' 'rand.unif.R' + 'rand.weib.R' 'tpower.R' + 'twodfunction.R' + 'twodranduphill.R' 'update.CompPack.R' + 'uphill.mixnorm.R' 'wich.hill.R' - 'pois.proc.R' - 'nearest.R' - 'around.R' - 'nearest.loc.R' + 'bootstrap.R' diff --git a/NAMESPACE b/NAMESPACE index 173e11e..4d2b219 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,18 +1,41 @@ +export(anneal) export(around) +export(bootstrap) +export(cvscore) +export(cvscore1) +export(cvscore2) +export(cvscore3) +export(cvscore4) +export(cvscore5) +export(d1f) +export(df) export(em.mixnorm) +export(f.models) +export(gold.sect) export(grid.sect) +export(jackknife) +export(log.lik) export(maxbound) export(mc) +export(mixlike) export(mixnorm) export(nearest) export(nearest.loc) export(newton.raph) +export(onedfunction) export(plot.mc) +export(pois.int) export(pois.proc) +export(pois.rate) +export(polar.rnorm) export(rand.exp) export(rand.mvnorm) export(rand.norm) export(rand.unif) +export(rand.weib) export(tpower) +export(twodfunction) +export(twodranduphill) export(update.CompPack) +export(uphill.mixnorm) export(wich.hill) diff --git a/R/.gitignore b/R/.gitignore new file mode 100644 index 0000000..62b29a2 --- /dev/null +++ b/R/.gitignore @@ -0,0 +1 @@ +rand.norm.R diff --git a/R/anneal.R b/R/anneal.R new file mode 100644 index 0000000..2103f99 --- /dev/null +++ b/R/anneal.R @@ -0,0 +1,42 @@ +#' uphill search on fucntion +#' +#' @description +#' General anneal function, where we conduct a uphill search on the function to find maximum +#' +#' @param f function to find the maximum +#' @param mu starting point +#' @param n number of iterations +#' @param sig size of steps +#' @param tt temperature +#' @param g rate of temperature +#' +#' @examples +#' anneal(onedfunction, 2, 100, 1, 10, .99) +#' @author Ernest Chan \email{faiernest418@@gmail.com} +#' +#' @export + +anneal = function(f,mu,n=1000,sig=1,tt=10,g=0.999) +{ + m = mu + fm = f(m) + x = rep(0,n) + fx = rep(0,n) + t = tt + for (i in 1:n) + { + dm = m+rnorm(1,0,sig) + fdm = f(dm) + + t = t*g + if (runif(1) < (fdm/fm)^(1/t)) + { + m = dm + fm = fdm + } + x[i] = m + fx[i] = fm + } + ii = ((1:n)[fx==max(fx)])[1] + list(x = x, fx = fx, best = x[ii], fbest = fx[ii], t=t) +} diff --git a/R/around.R b/R/around.R index e19578e..307a902 100644 --- a/R/around.R +++ b/R/around.R @@ -2,11 +2,19 @@ #' #' @description Find the location of values around a specified value #' -#' @param x a vector, matrix, or data frame. +#' @param x a vector. #' @param value specified value #' #' @author Tyler Hunt \email{tyler@@psychoanalytix.com} -#' @export +#' +#' @return lo the maximum value of x that is less than or equal to the value parameter. +#' @return hi the minimum value of x that is greater than or equal to the value parameter. +#' +#' @examples +#' x = rnorm(50, 3, 7) +#' value = 15 +#' +#' @export around<-function(x, value){ x<-sort(x) diff --git a/R/bootstrap.R b/R/bootstrap.R new file mode 100644 index 0000000..555f2fb --- /dev/null +++ b/R/bootstrap.R @@ -0,0 +1,24 @@ +#' bootstrap +#' +#' @description +#' This function is used for estimating standard errors when the distribution is not know. +#' +#' @param x a vector. +#' @param boots The number of bootstraps. +#' @param fn the function you want to bootstrap, ie., mean, var, cov, etc. +#' +#' @author Tyler Hunt \email{tyler@@psychoanalytix.com} +#' +#' @examples +#' x = runif(10, 0, 1) +#' bootstrap(x,fn=mean) +#' +#' @export + + +bootstrap<-function(x, boots=100, fn){ + n=length(x) + lings<-replicate(boots, fn(sample(x,n, replace=TRUE))) + + list(se=sd(lings), lings=lings) +} diff --git a/R/cvscore.R b/R/cvscore.R new file mode 100644 index 0000000..a729789 --- /dev/null +++ b/R/cvscore.R @@ -0,0 +1,96 @@ +#' Cross Validation +#' +#' @description Uses cross-validation to score how accurately a a linear, quadratic, cubic, and 4th and 5th degree polynomial models will perform when used in prediction. +#' +#' @param x is the independent variable +#' @param y is the dependent variable +#' +#' @author Katie Dodds \email{k.dodds@@utah.edu} +#' +#' @examples +#' attach(mtcars) +#' x=wt +#' y=mpg +#' cvscore(x,y) +#' +#' @export +cvscore<- +function(x,y){ +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*yi + param=glm(yi~xi)$coeff + yhat[i]=param[1]+x[i]*param[2] + } + linear=sum((yhat-y)^2) +} +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*yi + param=glm(yi~xi+zi)$coeff + yhat[i]=param[1]+x[i]*param[2]+x[i]^2*param[3] + } + quadratic=sum((yhat-y)^2) +} +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*xi + wi=xi*xi*xi + param=glm(yi~xi+zi+wi)$coeff + yhat[i]=param[1]+x[i]*param[2]+x[i]^2*param[3] +x[i]^3*param[4] + } + cubic=sum((yhat-y)^2) +} +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*xi + wi=xi*xi*xi + vi=xi*xi*xi*xi + param=glm(yi~xi+zi+wi+vi)$coeff + yhat[i]=param[1]+x[i]*param[2]+x[i]^2*param[3] +x[i]^3*param[4] +x[i]^4*param[5] + } + fourth.degree=sum((yhat-y)^2) +} +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*xi + wi=xi*xi*xi + vi=xi*xi*xi*xi + ui=xi^5 + param=glm(yi~xi+zi+wi+vi+ui)$coeff + yhat[i]=param[1]+x[i]*param[2]+x[i]^2*param[3] +x[i]^3*param[4] +x[i]^4*param[5]+x[i]^5*param[6] + } + fifth.degree=sum((yhat-y)^2) +} +list(linear=linear, quadratic=quadratic, cubic=cubic, fourth.degree, fifth.degree) +} diff --git a/R/cvscore1.R b/R/cvscore1.R new file mode 100644 index 0000000..3a1fad3 --- /dev/null +++ b/R/cvscore1.R @@ -0,0 +1,34 @@ +#' Cross Validation-Linear Model +#' +#' @description Uses cross-validation to score how accurately a linear model will perform when used in prediction. Score can be compared to that of cvscore2,...,cvscore5. +#' +#' @param x is the independent variable +#' @param y is the dependent variable +#' +#' @author Katie Dodds \email{k.dodds@@utah.edu} +#' +#' @examples +#' +#' attach(mtcars) +#' x=wt +#' y=mpg +#' cvscore1(x,y) +#' +#' @export + +cvscore1<- +function (x,y) +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*yi + param=glm(yi~xi)$coeff + yhat[i]=param[1]+x[i]*param[2] + } + sum((yhat-y)^2) +} diff --git a/R/cvscore2.R b/R/cvscore2.R new file mode 100644 index 0000000..bf91a56 --- /dev/null +++ b/R/cvscore2.R @@ -0,0 +1,33 @@ +#' Cross Validation-Quadratic Model +#' +#' @description Uses cross-validation to score how accurately a quadratic model will perform when used in prediction. Score can be compared to that of cvscore1,...,cvscore5. +#' +#' @param x is the independent variable +#' @param y is the dependent variable +#' +#' @author Katie Dodds \email{k.dodds@@utah.edu} +#' +#' @examples +#' +#' attach(mtcars) +#' x=wt +#' y=mpg +#' cvscore2(x,y) +#' +#' @export +cvscore2<- +function (x,y) +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*yi + param=glm(yi~xi+zi)$coeff + yhat[i]=param[1]+x[i]*param[2]+x[i]^2*param[3] + } + sum((yhat-y)^2) +} diff --git a/R/cvscore3.R b/R/cvscore3.R new file mode 100644 index 0000000..cecab16 --- /dev/null +++ b/R/cvscore3.R @@ -0,0 +1,33 @@ +#' Cross Validation-Cubic Model +#' +#' @description Uses cross-validation to score how accurately a cubic model will perform when used in prediction. Score can be compared to that of cvscore1,...,cvscore5. +#' +#' @param x is the independent variable +#' @param y is the dependent variable +#' +#' @author Katie Dodds \email{k.dodds@@utah.edu} +#' +#' @examples +#' +#' attach(mtcars) +#' x=wt +#' y=mpg +#' cvscore3(x,y) +#' +#' @export +cvscore3<-function (x,y) +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*xi + wi=xi*xi*xi + param=glm(yi~xi+zi+wi)$coeff + yhat[i]=param[1]+x[i]*param[2]+x[i]^2*param[3] +x[i]^3*param[4] + } + sum((yhat-y)^2) +} diff --git a/R/cvscore4.R b/R/cvscore4.R new file mode 100644 index 0000000..523890a --- /dev/null +++ b/R/cvscore4.R @@ -0,0 +1,35 @@ +#' Cross Validation-4th Degree Polynomial +#' +#' @description Uses cross-validation to score how accurately a model using a 4th degree polynomial will perform when used in prediction. Score can be compared to that of cvscore1,...,cvscore5. +#' +#' @param x is the independent variable +#' @param y is the dependent variable +#' +#' @author Katie Dodds \email{k.dodds@@utah.edu} +#' +#' @examples +#' +#' attach(mtcars) +#' x=wt +#' y=mpg +#' cvscore4(x,y) +#' +#' @export +cvscore4<- +function (x,y) +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*xi + wi=xi*xi*xi + vi=xi*xi*xi*xi + param=glm(yi~xi+zi+wi+vi)$coeff + yhat[i]=param[1]+x[i]*param[2]+x[i]^2*param[3] +x[i]^3*param[4] +x[i]^4*param[5] + } + sum((yhat-y)^2) +} diff --git a/R/cvscore5.R b/R/cvscore5.R new file mode 100644 index 0000000..9c7e4a8 --- /dev/null +++ b/R/cvscore5.R @@ -0,0 +1,36 @@ +#' Cross Validation-5th Degree Polynomial +#' +#' @description Uses cross-validation to score how accurately a model using a 5th degree polynomial will perform when used in prediction. Score can be compared to that of cvscore1,...,cvscore4. +#' +#' @param x is the independent variable +#' @param y is the dependent variable +#' +#' @author Katie Dodds \email{k.dodds@@utah.edu} +#' +#' @examples +#' +#' attach(mtcars) +#' x=wt +#' y=mpg +#' cvscore5(x,y) +#' +#' @export +cvscore5<- +function (x,y) +{ + n=length(x) + yhat=rep(0,n) + + for (i in 1:n) + { + yi=y[-i] + xi=x[-i] + zi=xi*xi + wi=xi*xi*xi + vi=xi*xi*xi*xi + ui=xi^5 + param=glm(yi~xi+zi+wi+vi+ui)$coeff + yhat[i]=param[1]+x[i]*param[2]+x[i]^2*param[3] +x[i]^3*param[4] +x[i]^4*param[5]+x[i]^5*param[6] + } + sum((yhat-y)^2) +} diff --git a/R/d1f.R b/R/d1f.R new file mode 100644 index 0000000..89b8df0 --- /dev/null +++ b/R/d1f.R @@ -0,0 +1,23 @@ +#' First Derivative +#' +#' @description +#' The first derivative of a function evaluated at point(s) x. +#' +#' Differentiation is a method to compute the rate at which the dependent output (f(x)) changes with respect to the change the independent input (x). The first derrivative can be used to obtain an exact value for the slope of the tangent line, at the point where the derivative was evaluated. +#' +#' @param f The function that you want to take the derivative of +#' @param x The point or vector of points where the derivative will be evaluated +#' +#' @author Damon McCafferty \email{damon.mccafferty@@economics.utah.edu} +#' +#' @examples +#' x = runif(10, pi/3, 3*pi/4) +#' df(tan, x) +#' +#' @export + +d1f <- function(f,x) +{ + d=0.0000001 + (f(x-d)-f(x))/d +} diff --git a/R/d2f.R b/R/d2f.R new file mode 100644 index 0000000..989e0cf --- /dev/null +++ b/R/d2f.R @@ -0,0 +1,23 @@ +#' Second Derivative +#' +#' @description +#' The Second derivative of a function evaluated at point(s) x. +#' +#' Roughly spoken, the seond derivative is the derivative of a derivative. This can be used to measure how the rate of change of a quantitty is itself changing. +#' +#' @param f The function that you want to take the derivative of +#' @param x The point or vector of points where the derivative will be evaluated +#' +#' @author Damon McCafferty \email{damon.mccafferty@@economics.utah.edu} +#' +#' @examples +#' x = runif(10, pi/3, 3*pi/4) +#' df(tan, x) +#' +#' @export + +df <- function(f,x) +{ + d=0.0000001 + (f(x-d)-2*f(x))/d/d +} diff --git a/R/em.mixnorm.R b/R/em.mixnorm.R index 98264e3..81f0663 100644 --- a/R/em.mixnorm.R +++ b/R/em.mixnorm.R @@ -1,10 +1,24 @@ #' EM-Algorithm for Normal Distribution +#' +#' @description +#' The Expectation-Maximization (EM) algorithm is and itterative method for locating the maximum estimate for the parameters of a mixture of normal distributions. The EM iteration alternates between performing an expectation (E) step, which creates a function for the expectation of the log-likelihood evaluated using the current estimate for the parameters, and a maximization (M) step, which computes parameters maximizing the expected log-likelihood found on the E step. These parameter-estimates #' #' @param x the data. #' @param k estimate of the number of mixtures. #' +#' @return itterations number of cycles the program used to calculate the estimated means, standard devaitions and probabilities. +#' @return means estimated mean of each mixture. +#' @return stddevs estimated standard deviation of each mixture. +#' @return probs estimated probabilities of each mixture. +#' #' @author Tyler Hunt \email{tyler@@psychoanalytix.com} +#' +#' @examples +#' x=mixnorm(100, c(.50, .15, .35), c(-3, 0, 3), c(3,3,3)) +#' em.mixnorm(x,3) +#' #' @export + em.mixnorm <- function (x,k){ p = rep(1/k,k) diff --git a/R/f.models.R b/R/f.models.R new file mode 100644 index 0000000..ae3e6a6 --- /dev/null +++ b/R/f.models.R @@ -0,0 +1,41 @@ +#' f models +#' +#' @description Plots linear, quadratic, cubic and 4th degree models for a 2D dataset. +#' +#' @param x is the independent variable +#' @param y is the dependent variable +#' +#' @author Katie Dodds \email{k.dodds@@utah.edu} +#' +#' @examples +#' attach(mtcars) +#' f.models(wt,mpg) +#' +#' @export + +f.models<- +function (x,y) +{ + par(mfrow=c(2,2)) + plot(x,y) + abline(lsfit(x,y),col=2) + + plot(x,y) + z=x*x + xx=seq(min(x)-10,max(x)+10,.1) + param=glm(y~x+z)$coeff + yy=param[1]+xx*param[2]+xx^2*param[3] + lines(xx,yy,col=3) + + plot(x,y) + w=x*x*x + param=glm(y~x+z+w)$coeff + yy=param[1]+xx*param[2]+xx^2*param[3]+xx^3*param[4] + lines(xx,yy,col=4) + + plot(x,y) + q=x*x*x*x + param=glm(y~x+z+w+q)$coeff + yy=param[1]+xx*param[2]+xx^2*param[3]+xx^3*param[4]+xx^4*param[5] + lines(xx,yy,col=5) +} diff --git a/R/gold.sect.R b/R/gold.sect.R index bddce10..c62fd6e 100644 --- a/R/gold.sect.R +++ b/R/gold.sect.R @@ -1,13 +1,22 @@ #' Golden Section Search Optimization +#' +#' @description +#' A method used for locating the optimum value of a function. The technique utilizes the golden ratio to successively narrow the range of values for which the optimum is found. #' #' @param f The function to be optimized #' @param lo Lower estimate #' @param hi Upper estimate #' @param tol Level of tolerance desired for optimization +#' +#' @return a estimated optimal value of the function +#' @return f(a) function value at the optimal value #' +#' @author Tyler Hunt \email{tyler@@psychoanalytix.com} +#' +#' @examples +#' gold.sect(sin, 1/2, pi/2) #' -#" @export - +#' @export gold.sect <- function(f,lo,hi,tol=0.0000001){ g = 1-(sqrt(5)-1)/2 diff --git a/R/grid.sect.R b/R/grid.sect.R index 1e13541..f393bde 100644 --- a/R/grid.sect.R +++ b/R/grid.sect.R @@ -1,8 +1,13 @@ #' Grid Section Search Optimization #' -#' @param f the function to be optimized -#' @param lo the lower bound -#' @param hi the upper bound +#' @description +#' A method used to locate the optimum value of a function. +#' +#' @param f the function to be optimized. +#' @param lo the lower bound. +#' @param hi the upper bound. +#' +#' @author Tyler Hunt \email{tyler@@psychoanalytix.com} #' #' @export diff --git a/R/jackknife.R b/R/jackknife.R new file mode 100644 index 0000000..c38bd98 --- /dev/null +++ b/R/jackknife.R @@ -0,0 +1,37 @@ +#' Resamples Data using the Jackknife Method +#' +#' @description +#' This function is used for estimating standard errors when the distribution is not know. +#' +#' @param x a vector +#' @param t estimation of parameter +#' +#' @return est orignial estimation of parameter +#' @return jkest jackknife estimation of parameter +#' @return jkvar jackknife estimation of variance +#' @return jkbias jackknife estimate of biasness of parameter +#' @return jkbiascorr bias corrected parameter estimate +#' +#' @author Damon McCafferty \email{damon.mccafferty@@economics.utah.edu} +#' +#' @examples x = runif(10, 0, 1) +#' mean(x) +#' jackknife(x,mean) +#' +#' @export + +jackknife<-function (x,t) +{ + n=length(x) + jk=rep(NA,n) + + for (i in 1:n) + { + jk[i]=t(x[-i]) + jkest=mean(jk) + jkvar=(n-1)/n*sum((jk-jkest)^2) + jkbias=(n-1)*(jkest-t(x)) + jkbiascorr=n*t(x)-(n-1)*jkest + } + list(est=t(x), jkest=jkest, jkvar=jkvar, jkbias=jkbias, jkbiascorr=jkbiascorr) +} diff --git a/R/log.lik.R b/R/log.lik.R new file mode 100644 index 0000000..f9018f4 --- /dev/null +++ b/R/log.lik.R @@ -0,0 +1,26 @@ +#' Find the log likelihood of a normal distribution +#' +#' @description +#' Find the log likelihood of a normal distribution. +#' +#' @param x data. +#' @param mu estimated mean. +#' @param var estimated variance. +#' +#' @author Damon McCafferty \email{damon.mccafferty@@economics.utah.edu} +#' +#' @return ll logliklihood of the distribution +#' +#' @examples +#' x = rnorm(100, 3, 7) +#' log.lik(x,3,7) +#' +#' @export + +log.lik<-function(x=data, mu, var) +{ + n=length(x) + ll = -n/2* log(2*pi*var) - .5/var*sum((mu-x)^2) + + -ll +} diff --git a/R/maxbound.R b/R/maxbound.R index fd6481e..126d182 100644 --- a/R/maxbound.R +++ b/R/maxbound.R @@ -1,9 +1,21 @@ #' Maxbound Optimization #' -#' @param f the function to be optimized +#' @description +#' Used to estimate the bounds of a function, in which the optimum value will be located within. +#' +#' @param f the function to be optimized. #' @param lo the lower bound. #' @param hi the upper bound. #' +#' @return lo lower bound estimate of the function's optimum. +#' @return hi upper bound estimate of the function's optimum. +#' +#' @author Tyler Hunt \email{tyler@@psychoanalytix.com} +#' +#' @examples +#' maxbound(sin, 1/2, pi/2) +#' +#' #' @export maxbound <- diff --git a/R/mc.R b/R/mc.R index 55c6b50..b6b975b 100644 --- a/R/mc.R +++ b/R/mc.R @@ -1,8 +1,12 @@ #' Generate Markov Chains #' +#' @description +#' Generates a Markov Chain. #' -#' @param p Probability matrix -#' @param n Number of observations +#' @param p Probability matrix. +#' @param n Number of observations. +#' +#' @return x values of observations. #' #' @author Tyler Hunt \email{tyler@@psychoanalytix.com} #' @@ -15,6 +19,7 @@ #' mc(B, 10) #' mc(C, 10) #' mc(D, 10) +#' #' @export mc<-function(p, n){ diff --git a/R/mixlike.R b/R/mixlike.R new file mode 100644 index 0000000..ecf6e31 --- /dev/null +++ b/R/mixlike.R @@ -0,0 +1,36 @@ +#' mixlike +#' +#' @description +#' A supplement fucntion for uphill.mixnorm, this use to compute the log likelihood funciotn for the mixture of normal distribution +#' +#' @param x is the mixture function to be evaluated +#' @param p is the vector of probabilities +#' @param m is the vector if means +#' @param s is the vector of standard deviation +#' +#' @examples +#' mixlike(mixnorm(100, c(.50, .15, .35), c(-3, 0, 3), c(3,3,3)), c(.5,.5), c(-5,5), c(1,1)) +#' +#' @author Ernest Chan \email{faiernest418@@gmail.com} +#' +#' @export + +mixlike = function(x, p, m, s) +{ + ll = 0 + + for (i in 1:length(p)) + { + fi = 0 + + for (j in 1:length(p)) + { + fi = fi + p[j] * dnorm(x[i], m[j], s[j]) + } + + ll = ll + log(fi) + } + + ll + +} diff --git a/R/mixnorm.R b/R/mixnorm.R index 13e5ef5..097ab90 100644 --- a/R/mixnorm.R +++ b/R/mixnorm.R @@ -1,11 +1,19 @@ #' Create a Mixture of Normal Distributions +#' +#' @description +#' Creates a mixture of normal distributions. #' -#' @param n number of observations. If \eqn{length(n) > 1}, the length is taken to be the number required -#' @param p vector of probabilities -#' @param mu vector of means -#' @param sd vector of standard deviations +#' @param n number of observations. If \eqn{length(n) > 1}, the length is taken to be the number required. +#' @param p vector of probabilities. +#' @param mu vector of means. +#' @param sd vector of standard deviations. #' #' @author Tyler Hunt \email{tyler@@psychoanalytix.com} +#' +#' @examples +#' x = mixnorm(100, c(.50, .15, .35), c(-3, 0, 3), c(3,3,3)) +#' hist(x) +#' #' @export mixnorm <- diff --git a/R/newton.raph.R b/R/newton.raph.R index c82150d..4af0407 100644 --- a/R/newton.raph.R +++ b/R/newton.raph.R @@ -1,24 +1,30 @@ #' Newton Raphson Alogorithm #' +#' @description +#' A method for obtaining successively better approximations for the optimal value of a function. Named after Sir Isaac Newton and Joseph Raphson. +#' #' @param f The function that you want to optimize. -#' @param df the 1st derivative of the function. +#' @param d1f the 1st derivative of the function. #' @param d2f the 2nd derivative of the function. #' @param start The starting value for optimization. #' @param tol The level of tolerance desired for convergence. #' +#' @return Estimate Initial estimate of the optimum value. +#' @return Iterations Number of cycles used to obtain the optimum value of the function. +#' @return Liklihood Optimum value #' #' @author Tyler Hunt \email{tyler@@psychoanalytix.com} #' @export -newton.raph <- function(f, df, d2f, start, tol=0.000001){ +newton.raph <- function(f, d1f, d2f, start, tol=0.000001){ new <- start + 10*tol iter <- 0 while(abs(start - new) > tol){ iter <- iter+1 new <- start - start <- start - df(start)/d2f(start) + start <- start - d1f(start)/d2f(start) } result<-c(Estimate=start, Iterations=iter, Likelihood=f(start)) return(result) diff --git a/R/onedfunction.R b/R/onedfunction.R new file mode 100644 index 0000000..a3e2e03 --- /dev/null +++ b/R/onedfunction.R @@ -0,0 +1,20 @@ +#' x^4 + x^2 +#' +#' @description +#' function to generate x^4 + x^2 +#' +#' @param x vector of values +#' +#' @examples +#' onedfunction(1) +#' +#' @author Ernest Chan \email{faiernest418@@gmail.com} +#' +#' @export + +onedfunction = function(x) +{ + y = x^4 + x^2 + + y +} diff --git a/R/pois.int.R b/R/pois.int.R new file mode 100644 index 0000000..17c9385 --- /dev/null +++ b/R/pois.int.R @@ -0,0 +1,26 @@ +#' pois.int +#' +#' @description Averages the length of the interval containing a specified time +#' +#' @param time is the total time. +#' @param r is the average number of events per unit in time +#' @param n is the number of simulations required +#' @param a is the time the estimated interval contains +#' +#' @author Katie Dodds \email{k.dodds@@utah.edu} +#' +#' @examples +#' +#' pois.int(100,5,1000,100) +#' +#' @export +pois.int<- +function(time,r,n=1000,a){ + z=0 + for(i in 1:n){ + x=pois.proc(time,r) + y=min(x[x>a])-max(x[x 1}, - the length is taken to be the number required} + the length is taken to be the number required.} - \item{p}{vector of probabilities} + \item{p}{vector of probabilities.} - \item{mu}{vector of means} + \item{mu}{vector of means.} - \item{sd}{vector of standard deviations} + \item{sd}{vector of standard deviations.} } \description{ - Create a Mixture of Normal Distributions + Creates a mixture of normal distributions. +} +\examples{ +x = mixnorm(100, c(.50, .15, .35), c(-3, 0, 3), c(3,3,3)) +hist(x) } \author{ Tyler Hunt \email{tyler@psychoanalytix.com} diff --git a/man/newton.raph.Rd b/man/newton.raph.Rd index 53667f8..2b9a564 100644 --- a/man/newton.raph.Rd +++ b/man/newton.raph.Rd @@ -2,12 +2,12 @@ \alias{newton.raph} \title{Newton Raphson Alogorithm} \usage{ - newton.raph(f, df, d2f, start, tol = 1e-06) + newton.raph(f, d1f, d2f, start, tol = 1e-06) } \arguments{ \item{f}{The function that you want to optimize.} - \item{df}{the 1st derivative of the function.} + \item{d1f}{the 1st derivative of the function.} \item{d2f}{the 2nd derivative of the function.} @@ -16,8 +16,18 @@ \item{tol}{The level of tolerance desired for convergence.} } +\value{ + Estimate Initial estimate of the optimum value. + + Iterations Number of cycles used to obtain the optimum + value of the function. + + Liklihood Optimum value +} \description{ - Newton Raphson Alogorithm + A method for obtaining successively better approximations + for the optimal value of a function. Named after Sir + Isaac Newton and Joseph Raphson. } \author{ Tyler Hunt \email{tyler@psychoanalytix.com} diff --git a/man/onedfunction.Rd b/man/onedfunction.Rd new file mode 100644 index 0000000..e19c2e8 --- /dev/null +++ b/man/onedfunction.Rd @@ -0,0 +1,19 @@ +\name{onedfunction} +\alias{onedfunction} +\title{x^4 + x^2} +\usage{ + onedfunction(x) +} +\arguments{ + \item{x}{vector of values} +} +\description{ + function to generate x^4 + x^2 +} +\examples{ +onedfunction(1) +} +\author{ + Ernest Chan \email{faiernest418@gmail.com} +} + diff --git a/man/pois.int.Rd b/man/pois.int.Rd new file mode 100644 index 0000000..2c7bc41 --- /dev/null +++ b/man/pois.int.Rd @@ -0,0 +1,27 @@ +\name{pois.int} +\alias{pois.int} +\title{pois.int} +\usage{ + pois.int(time, r, n = 1000, a) +} +\arguments{ + \item{time}{is the total time.} + + \item{r}{is the average number of events per unit in + time} + + \item{n}{is the number of simulations required} + + \item{a}{is the time the estimated interval contains} +} +\description{ + Averages the length of the interval containing a + specified time +} +\examples{ +pois.int(100,5,1000,100) +} +\author{ + Katie Dodds \email{k.dodds@utah.edu} +} + diff --git a/man/pois.proc.Rd b/man/pois.proc.Rd index 8399e2b..2290aa4 100644 --- a/man/pois.proc.Rd +++ b/man/pois.proc.Rd @@ -1,21 +1,22 @@ \name{pois.proc} \alias{pois.proc} -\title{Generate a Poisson Process} +\title{pois.proc} \usage{ - pois.proc(end = 200, rate = 1) + pois.proc(time, r) } \arguments{ - \item{end}{the end time.} + \item{time}{is the total time.} - \item{rate}{the rate of occurrence.} + \item{r}{is the average number of events.} } \description{ - Generate a Poisson Process + Simulates a Poisson process of specified length and rate. + The output is the time at which each event occured. } \examples{ -pois.proc(200, 1) +pois.proc(100,5) } \author{ - Tyler Hunt \email{tyler@psychoanalytix.com} + Katie Dodds \email{k.dodds@utah.edu} } diff --git a/man/pois.rate.Rd b/man/pois.rate.Rd new file mode 100644 index 0000000..ae54462 --- /dev/null +++ b/man/pois.rate.Rd @@ -0,0 +1,27 @@ +\name{pois.rate} +\alias{pois.rate} +\title{Average Time Between Events in a Poisson Process} +\usage{ + pois.rate(time, r, n = 1000, a, b) +} +\arguments{ + \item{time}{is the total time} + + \item{r}{is the average number of events} + + \item{n}{is the number if simulations required} + + \item{a,b}{are the events being compared} +} +\description{ + Averages the time between any two events in a Poisson + procces +} +\examples{ +pois.rate(100,5,1000,90,100) +## Takes the average time between the 90th and 100th events +} +\author{ + Katie Dodds \email{k.dodds@utah.edu} +} + diff --git a/man/polar.rnorm.Rd b/man/polar.rnorm.Rd new file mode 100644 index 0000000..11a06ea --- /dev/null +++ b/man/polar.rnorm.Rd @@ -0,0 +1,24 @@ +\name{polar.rnorm} +\alias{polar.rnorm} +\title{Polar Random Normal} +\usage{ + polar.rnorm(n = 100, mu = 0, sd = 1) +} +\arguments{ + \item{n}{is the sample size} + + \item{mu}{a vector of means} + + \item{sd}{a vector of standard deviations} +} +\description{ + Generates a sample of standard normals +} +\examples{ +polar.rnorm(100) +} +\author{ + Katie Dodds \email{k.dodds@utah.edu} Damon McCafferty + \email{damon.mccafferty@economics.utah.edu} +} + diff --git a/man/rand.exp.Rd b/man/rand.exp.Rd index f357d6e..fa5f2ad 100644 --- a/man/rand.exp.Rd +++ b/man/rand.exp.Rd @@ -10,7 +10,8 @@ \item{rate}{vector of rates} } \description{ - Generate Random Exponential Variables + Generates a pseudo-random exponential variable using the + Whichman-Hill pseudo-random number generator function } \author{ Tyler Hunt \email{tyler@psychoanalytix.com} diff --git a/man/rand.mvnorm.Rd b/man/rand.mvnorm.Rd index 3b970d0..b6af3fd 100644 --- a/man/rand.mvnorm.Rd +++ b/man/rand.mvnorm.Rd @@ -5,15 +5,15 @@ rand.mvnorm(n, mu, sigma) } \arguments{ - \item{n}{the number of observations} + \item{n}{the number of observations.} - \item{mu}{a vector of means} + \item{mu}{a vector of means.} - \item{sigma}{a variance-covariance matrix} + \item{sigma}{a variance-covariance matrix.} } \description{ - A method for generating random multivariate normal data - that is based on a cholesky decomposition. + A method for generating a pseudo-random multivariate + normal data that is based on a cholesky decomposition. } \examples{ sigma=matrix(rep(.5, 9), nrow=3) diff --git a/man/rand.norm.Rd b/man/rand.norm.Rd index 7f18957..e001b8c 100644 --- a/man/rand.norm.Rd +++ b/man/rand.norm.Rd @@ -5,14 +5,15 @@ rand.norm(n, mean, sd) } \arguments{ - \item{n}{Number of observations} + \item{n}{Number of observations.} - \item{mean}{vector of means} + \item{mean}{vector of means.} - \item{sd}{vector of standard deviations} + \item{sd}{vector of standard deviations.} } \description{ - Generate Random Normal Variables + Generates pseudo random variables from a normal + distribution using the Wichmannhill. } \author{ Tyler Hunt \email{tyler@psychoanalytix.com} diff --git a/man/rand.unif.Rd b/man/rand.unif.Rd index 9f10463..f01d2d5 100644 --- a/man/rand.unif.Rd +++ b/man/rand.unif.Rd @@ -5,14 +5,15 @@ rand.unif(n, min = 0, max = 1) } \arguments{ - \item{n}{Number of observations} + \item{n}{Number of observations.} - \item{min}{lower limit of the distribution} + \item{min}{lower limit of the distribution.} - \item{max}{upper limit of the distribution} + \item{max}{upper limit of the distribution.} } \description{ - Generate Random Uniform Variables + Generates pseudo-random variables from a Uniform + distribution, using the Wichmannhill. } \author{ Tyler Hunt \email{tyler@psychoanalytix.com} diff --git a/man/rand.weib.Rd b/man/rand.weib.Rd new file mode 100644 index 0000000..ac1b998 --- /dev/null +++ b/man/rand.weib.Rd @@ -0,0 +1,20 @@ +\name{rand.weib} +\alias{rand.weib} +\title{Generate Random Weibull Variables} +\usage{ + rand.weib(n, beta, theta) +} +\arguments{ + \item{n}{the number of observations} + + \item{beta}{the specification of beta} + + \item{theta}{the specification of theta} +} +\description{ + Generate Random Weibull Variables +} +\author{ + Tyler Hunt \email{tyler@psychoanalytix.com} +} + diff --git a/man/twodfunction.Rd b/man/twodfunction.Rd new file mode 100644 index 0000000..2001317 --- /dev/null +++ b/man/twodfunction.Rd @@ -0,0 +1,21 @@ +\name{twodfunction} +\alias{twodfunction} +\title{exp(-(x+y))} +\usage{ + twodfunction(x, y) +} +\arguments{ + \item{x}{vector of value} + + \item{y}{vector of value} +} +\description{ + Generate exp(-(x+y) maxtrix +} +\examples{ +twodfunction(1,1) +} +\author{ + Ernest Chan \email{faiernest@gmail.com} +} + diff --git a/man/twodranduphill.Rd b/man/twodranduphill.Rd new file mode 100644 index 0000000..054bb12 --- /dev/null +++ b/man/twodranduphill.Rd @@ -0,0 +1,31 @@ +\name{twodranduphill} +\alias{twodranduphill} +\title{uphill search on matrix function} +\usage{ + twodranduphill(f, x, y, s, n = 100, t = 100) +} +\arguments{ + \item{f}{matrix of data set} + + \item{y}{is the starting point for the search} + + \item{x}{is the starting point for the search} + + \item{n}{is the number of iteration for the search} + + \item{s}{standard devation for each search jump, default + at 1} + + \item{t}{is the tempearture, where its default at 100} +} +\description{ + This function is use to do a random uphill search on any + 2 demision data set +} +\examples{ +twodranduphill(twodfunction, 3, 2, 1, 10, 1) +} +\author{ + Ernest Chan \email{faiernest418@gmail.com} +} + diff --git a/man/uphill.mixnorm.Rd b/man/uphill.mixnorm.Rd new file mode 100644 index 0000000..4a077fa --- /dev/null +++ b/man/uphill.mixnorm.Rd @@ -0,0 +1,26 @@ +\name{uphill.mixnorm} +\alias{uphill.mixnorm} +\title{uphill.mixnorm} +\usage{ + uphill.mixnorm(x, k, n = 1000, t = 100) +} +\arguments{ + \item{x}{is the function of the mixture} + + \item{k}{is the number of mixture} + + \item{n}{is the number of iteration} + + \item{t}{is the temperature} +} +\description{ + This function is use for conducting a uphill search on + the mixture of normal distribution +} +\examples{ +uphill.mixnorm(mixnorm(100, c(.50, .15, .35), c(-3, 0, 3), c(3,3,3)), 4, 1000, 10) +} +\author{ + Ernest Chan \email{faiernest418@gmail.com} +} +