├── demo ├── 00Index └── demo.R ├── data ├── IT.rda ├── apple.rda ├── Dosing.rda ├── Income.rda ├── LMdata.rda ├── mapdata.rda ├── titanic.rda ├── SogouCS08.rda └── EarthquakeCN.rda ├── NAMESPACE ├── inst └── examples │ ├── db │ └── irisdb.db │ ├── gis │ ├── hubei.dbf │ ├── hubei.shp │ └── hubei.shx │ ├── web │ └── jywg.scel │ ├── sna │ └── lijian001.txt │ ├── images │ └── sorghum.jpg │ └── optimization │ └── sudoku.mod ├── R ├── zzz.R ├── bestCluster.R ├── bsoption.R ├── bin2int.R ├── utils.R ├── getOANDA.R ├── extractSudoku.R ├── int2bin.R ├── biOps2Image.R ├── Image2biOps.R ├── sign.test.R ├── emax.fit.R ├── plotTS.R └── installAllDSPkgs.R ├── .gitignore ├── DESCRIPTION ├── man ├── SogouCS08.Rd ├── plotTS.Rd ├── bin2int.Rd ├── biOps2Image.Rd ├── Image2biOps.Rd ├── int2bin.Rd ├── loadAllDSPkgs.Rd ├── EarthquakeCN.Rd └── sign.test.Rd └── LICENSE /demo/00Index: -------------------------------------------------------------------------------- 1 | demo demos 2 | 3 | -------------------------------------------------------------------------------- /data/IT.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/data/IT.rda -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | 2 | # exportPattern(".") 3 | exportPattern("^[^\\.]") 4 | 5 | -------------------------------------------------------------------------------- /data/apple.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/data/apple.rda -------------------------------------------------------------------------------- /data/Dosing.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/data/Dosing.rda -------------------------------------------------------------------------------- /data/Income.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/data/Income.rda -------------------------------------------------------------------------------- /data/LMdata.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/data/LMdata.rda -------------------------------------------------------------------------------- /data/mapdata.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/data/mapdata.rda -------------------------------------------------------------------------------- /data/titanic.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/data/titanic.rda -------------------------------------------------------------------------------- /data/SogouCS08.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/data/SogouCS08.rda -------------------------------------------------------------------------------- /data/EarthquakeCN.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/data/EarthquakeCN.rda -------------------------------------------------------------------------------- /inst/examples/db/irisdb.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/inst/examples/db/irisdb.db -------------------------------------------------------------------------------- /inst/examples/gis/hubei.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/inst/examples/gis/hubei.dbf -------------------------------------------------------------------------------- /inst/examples/gis/hubei.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/inst/examples/gis/hubei.shp -------------------------------------------------------------------------------- /inst/examples/gis/hubei.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/inst/examples/gis/hubei.shx -------------------------------------------------------------------------------- /inst/examples/web/jywg.scel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/inst/examples/web/jywg.scel -------------------------------------------------------------------------------- /inst/examples/sna/lijian001.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/inst/examples/sna/lijian001.txt -------------------------------------------------------------------------------- /inst/examples/images/sorghum.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijian13/rinds/HEAD/inst/examples/images/sorghum.jpg -------------------------------------------------------------------------------- /demo/demo.R: -------------------------------------------------------------------------------- 1 | 2 | library(rinds) 3 | 4 | # install all the packages this book uses: 5 | 6 | loadAllDSPkgs(install = TRUE) 7 | 8 | 9 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | 2 | .onAttach <- function(libname, pkgname ){ 3 | packageStartupMessage( paste("# Version:", packageDescription("rinds", fields = "Version"), sep = "") ) 4 | 5 | } 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # History files 2 | .Rhistory 3 | 4 | # Example code in package build process 5 | *-Ex.R 6 | 7 | # R data files from past sessions 8 | .Rdata 9 | 10 | # RStudio files 11 | .Rproj.user/ 12 | /.project 13 | -------------------------------------------------------------------------------- /R/bestCluster.R: -------------------------------------------------------------------------------- 1 | 2 | bestCluster <- function(data, n, plot = FALSE) { 3 | clusting <- lapply(n,function(x) pam(data,k=x)) 4 | silwidth <- lapply(clusting,function(x) 5 | summary(silhouette(x))$avg.width) 6 | bestn <- n[which.max(silwidth)] 7 | if (plot) plot(x = n, y = silwidth, type = 'b') 8 | return(bestn) 9 | } 10 | -------------------------------------------------------------------------------- /R/bsoption.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | bsoption <- function(s,x,r,t,sigma,call=TRUE){ 5 | d1 <- (log(s/x)+(r+sigma^2/2)*t)/(sigma*sqrt(t)) 6 | d2 <- d1-sigma*sqrt(t) 7 | if(call) { 8 | c <- s*pnorm(d1)-x*exp(-r*t)*pnorm(d2) 9 | return(c) 10 | } else { 11 | p <- x*exp(-r*t)*pnorm(-d2)-s*pnorm(-d1) 12 | return(p) 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: rinds 2 | License: LGPL 3 | Title: Functions and datasets for the book "R in Data Science" 4 | Type: Package 5 | LazyLoad: yes 6 | Author: Jian Li and Kai Xiao 7 | Maintainer: Jian Li 8 | Description: This package contains all the example datasets and functions in the book and also be a toolkit of data science. 9 | Version: 1.0-601 10 | Date: 2015-07-06 11 | Depends: R (>= 2.12.0), utils 12 | 13 | -------------------------------------------------------------------------------- /R/bin2int.R: -------------------------------------------------------------------------------- 1 | 2 | ##' Convert binary mode to integers. 3 | ##' 4 | ##' @title Convert binary mode to integers. 5 | ##' @param bin string vector in binary mode. 6 | ##' @return A numeric vector. 7 | ##' @author Jian Li <\email{rinds.book@@gmail.com}> 8 | ##' @keywords string 9 | ##' @examples \dontrun{ 10 | ##' bin2int("001101") 11 | ##' } 12 | ##' 13 | 14 | bin2int <- function(bin) 15 | { 16 | res <- strsplit(bin, split = "") 17 | return(sapply(res, FUN = function(X) sum(as.numeric(X)*2^rev(seq_along(X))))/2) 18 | } 19 | 20 | -------------------------------------------------------------------------------- /man/SogouCS08.Rd: -------------------------------------------------------------------------------- 1 | \name{SogouCS08} 2 | \alias{SogouCS08} 3 | \docType{data} 4 | \title{Sogou news data set} 5 | \description{ 6 | News in 2008 from Sogou lab. 7 | } 8 | \usage{data(SogouCS08)} 9 | \format{ 10 | A data frame with 2 columns. 11 | \describe{ 12 | \item{\code{Title}}{Title of news.} 13 | \item{\code{Content}}{Content of news.} 14 | } 15 | } 16 | 17 | \author{ 18 | Jian Li <\email{rinds.book@gmail.com}> 19 | } 20 | \references{ 21 | \url{http://www.sogou.com/labs/dl/cs.html} 22 | } 23 | \keyword{datasets} 24 | -------------------------------------------------------------------------------- /man/plotTS.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.1.0): do not edit by hand 2 | % Please edit documentation in R/plotTS.R 3 | \name{plotTS} 4 | \alias{plotTS} 5 | \title{Plot time series and its forecast values.} 6 | \usage{ 7 | plotTS(series, forecast) 8 | } 9 | \arguments{ 10 | \item{series}{ts object.} 11 | 12 | \item{forecast}{the output of predict or a forecast object.} 13 | } 14 | \value{ 15 | plot. 16 | } 17 | \description{ 18 | Plot time series and its forecast values. 19 | } 20 | \author{ 21 | Jian Li <\email{rinds.book@gmail.com}> 22 | } 23 | 24 | -------------------------------------------------------------------------------- /man/bin2int.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.1.0): do not edit by hand 2 | % Please edit documentation in R/bin2int.R 3 | \name{bin2int} 4 | \alias{bin2int} 5 | \title{Convert binary mode to integers.} 6 | \usage{ 7 | bin2int(bin) 8 | } 9 | \arguments{ 10 | \item{bin}{string vector in binary mode.} 11 | } 12 | \value{ 13 | A numeric vector. 14 | } 15 | \description{ 16 | Convert binary mode to integers. 17 | } 18 | \examples{ 19 | \dontrun{ 20 | bin2int("001101") 21 | } 22 | } 23 | \author{ 24 | Jian Li <\email{rinds.book@gmail.com}> 25 | } 26 | \keyword{string} 27 | 28 | -------------------------------------------------------------------------------- /R/utils.R: -------------------------------------------------------------------------------- 1 | 2 | .installPkg <- function(pkg, lib.loc, install, ...) { 3 | 4 | tmp.load <- FALSE 5 | eval(parse(text = paste("tmp.load <- suppressWarnings(require(", 6 | pkg, ", lib.loc = '", lib.loc, 7 | "', warn.conflicts = FALSE))", sep = ""))) 8 | 9 | tmp.calls <- as.list(match.call()) 10 | tmp.calls[[1]] <- NULL 11 | tmp.calls[["pkg"]] <- NULL 12 | tmp.calls[["lib.loc"]] <- NULL 13 | tmp.calls[["install"]] <- NULL 14 | tmp.calls[["pkgs"]] <- pkg 15 | tmp.calls[["lib"]] <- lib.loc 16 | 17 | if (!tmp.load && install) do.call("install.packages", tmp.calls) 18 | } 19 | -------------------------------------------------------------------------------- /man/biOps2Image.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.1.0): do not edit by hand 2 | % Please edit documentation in R/biOps2Image.R 3 | \name{biOps2Image} 4 | \alias{biOps2Image} 5 | \title{Convert imagedata object of biOps package to Image object of EBImage package.} 6 | \usage{ 7 | biOps2Image(x) 8 | } 9 | \arguments{ 10 | \item{x}{imagedata object of biOps package.} 11 | } 12 | \value{ 13 | Image object. 14 | } 15 | \description{ 16 | Convert imagedata object to Image object. 17 | } 18 | \examples{ 19 | \dontrun{ 20 | biOps2Image(x) 21 | } 22 | } 23 | \author{ 24 | Jian Li <\email{rinds.book@gmail.com}> 25 | } 26 | 27 | -------------------------------------------------------------------------------- /man/Image2biOps.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.1.0): do not edit by hand 2 | % Please edit documentation in R/Image2biOps.R 3 | \name{Image2biOps} 4 | \alias{Image2biOps} 5 | \title{Convert Image object of EBImage package to imagedata object of biOps package.} 6 | \usage{ 7 | Image2biOps(x) 8 | } 9 | \arguments{ 10 | \item{x}{Image object of EBImage package.} 11 | } 12 | \value{ 13 | imagedata object. 14 | } 15 | \description{ 16 | Convert Image object to imagedata object. 17 | } 18 | \examples{ 19 | \dontrun{ 20 | Image2biOps(x) 21 | } 22 | } 23 | \author{ 24 | Jian Li <\email{rinds.book@gmail.com}> 25 | } 26 | 27 | -------------------------------------------------------------------------------- /man/int2bin.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.1.0): do not edit by hand 2 | % Please edit documentation in R/int2bin.R 3 | \name{int2bin} 4 | \alias{int2bin} 5 | \title{Convert integers to binary mode.} 6 | \usage{ 7 | int2bin(int, len = NULL) 8 | } 9 | \arguments{ 10 | \item{int}{integer vector.} 11 | 12 | \item{len}{number of characters of the result string.} 13 | } 14 | \value{ 15 | A character vector. 16 | } 17 | \description{ 18 | Convert integers to binary mode. 19 | } 20 | \examples{ 21 | \dontrun{ 22 | int2bin(10) 23 | } 24 | } 25 | \author{ 26 | Jian Li <\email{rinds.book@gmail.com}> 27 | } 28 | \keyword{string} 29 | 30 | -------------------------------------------------------------------------------- /R/getOANDA.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | getOANDA <- function(symbol="USD/EUR",from="2005-01-01",to="2012-01-01") { 5 | from <- as.Date(from) 6 | to <- as.Date(to) 7 | span <- as.numeric(to-from) 8 | n <- span %/% 500 9 | rem <- span %% 500 10 | res <- NULL 11 | for (i in 1:n) { 12 | mid <- to - 499 13 | data <- getSymbols(symbol,src="oanda", 14 | from=as.character(mid), 15 | to=as.character(to), 16 | auto.assign=F) 17 | res <- rbind(res,data) 18 | to <- mid - 1 19 | } 20 | from <- mid - rem -1 21 | data <- getSymbols(symbol,src="oanda", 22 | from=as.character(from), 23 | to=as.character(to), 24 | auto.assign=F) 25 | res <- rbind(res,data) 26 | return(res) 27 | } 28 | 29 | -------------------------------------------------------------------------------- /R/extractSudoku.R: -------------------------------------------------------------------------------- 1 | 2 | extractSudoku <- function(solution) { 3 | res.vals <- sapply(split(solution, f = rep(1:81, each = 9)), FUN = function(X) which(X > 0)) 4 | res.prob <- matrix(NA, 9, 9) 5 | res.prob[1, c(1, 2, 5)] <- c(5, 3, 7) 6 | res.prob[2, c(1, 4, 5, 6)] <- c(6, 1, 9, 5) 7 | res.prob[3, c(2, 3, 8)] <- c(9, 8, 6) 8 | res.prob[4, c(1, 5, 9)] <- c(8, 6, 3) 9 | res.prob[5, c(1, 4, 6, 9)] <- c(4, 8, 3, 1) 10 | res.prob[6, c(1, 5, 9)] <- c(7, 2, 6) 11 | res.prob[7, c(2, 7, 8)] <- c(6, 2, 8) 12 | res.prob[8, c(4, 5, 6, 9)] <- c(4, 1, 9, 5) 13 | res.prob[9, c(5, 8, 9)] <- c(8, 7, 9) 14 | res.m.t <- t(res.prob) 15 | res.m.t[is.na(res.m.t)] <- res.vals[-(1:sum(!is.na(res.prob)))] 16 | res.m <- t(res.m.t) 17 | res.m 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /R/int2bin.R: -------------------------------------------------------------------------------- 1 | 2 | ##' Convert integers to binary mode. 3 | ##' 4 | ##' @title Convert integers to binary mode. 5 | ##' @param int integer vector. 6 | ##' @param len number of characters of the result string. 7 | ##' @return A character vector. 8 | ##' @author Jian Li <\email{rinds.book@@gmail.com}> 9 | ##' @keywords string 10 | ##' @examples \dontrun{ 11 | ##' int2bin(10) 12 | ##' } 13 | ##' 14 | 15 | int2bin <- function(int, len = NULL) 16 | { 17 | res <- character(length(int)) 18 | tmp <- NULL 19 | while (any(int > 0) | is.null(tmp)) { 20 | tmp <- int%%2 21 | int <- floor(int/2) 22 | res <- paste(tmp, res, sep = "") 23 | } 24 | if (identical(class(len), "numeric")) res <- sprintf(paste("%0", len, "d", sep = ""), as.numeric(res)) 25 | return(res) 26 | } 27 | 28 | -------------------------------------------------------------------------------- /man/loadAllDSPkgs.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.1.0): do not edit by hand 2 | % Please edit documentation in R/installAllDSPkgs.R 3 | \name{loadAllDSPkgs} 4 | \alias{loadAllDSPkgs} 5 | \title{Load all the required packages mentioned in this book.} 6 | \usage{ 7 | loadAllDSPkgs(install = FALSE, lib.loc = .libPaths()[1]) 8 | } 9 | \arguments{ 10 | \item{install}{Whether to install the uninstalled packages, default is FALSE.} 11 | 12 | \item{lib.loc}{character vector giving the library directories where to install the packages. Defaults to the first element of \code{\link{.libPaths}()}.} 13 | } 14 | \value{ 15 | TRUE or FALSE. 16 | } 17 | \description{ 18 | Load all the required packages mentioned in this book. 19 | } 20 | \examples{ 21 | \dontrun{ 22 | loadAllDSPkgs(install = TRUE) 23 | } 24 | } 25 | \author{ 26 | Jian Li <\email{rinds.book@gmail.com}> 27 | } 28 | \keyword{string} 29 | 30 | -------------------------------------------------------------------------------- /man/EarthquakeCN.Rd: -------------------------------------------------------------------------------- 1 | \name{EarthquakeCN} 2 | \alias{EarthquakeCN} 3 | \docType{data} 4 | \title{Earth quake data set} 5 | \description{ 6 | Earth quake data set of China and its surrounding areas from 2014-09-08 to 2014-09-12. 7 | } 8 | \usage{data(EarthquakeCN)} 9 | \format{ 10 | A data frame with 6 columns. 11 | \describe{ 12 | \item{\code{Time}}{Time of the earth quake.} 13 | \item{\code{Lat}}{Latitude of the earth quake.} 14 | \item{\code{Long}}{Longitude of the earth quake.} 15 | \item{\code{Dep}}{Depth of the earth quake.} 16 | \item{\code{Mag}}{Magnitude of the earth quake on the Richter scale.} 17 | \item{\code{Loc}}{Location of the earth quake.} 18 | } 19 | } 20 | 21 | \author{ 22 | Jian Li <\email{rinds.book@gmail.com}> 23 | } 24 | 25 | \references{ 26 | \url{http://data.earthquake.cn/datashare/globeEarthquake_csn.html} 27 | } 28 | 29 | \keyword{datasets} 30 | -------------------------------------------------------------------------------- /man/sign.test.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.1.0): do not edit by hand 2 | % Please edit documentation in R/sign.test.R 3 | \name{sign.test} 4 | \alias{sign.test} 5 | \title{Sign test.} 6 | \usage{ 7 | \method{sign}{test}(v, m0, h1 = c("two.sided", "less", "greater"), 8 | EXACT = c("exact", "approximation")) 9 | } 10 | \arguments{ 11 | \item{v}{numeric vector of data values. Non-finite (e.g. infinite or missing) values will be omitted.} 12 | 13 | \item{m0}{a number specifying an optional parameter used to form the null hypothesis.} 14 | 15 | \item{h1}{a character string specifying the alternative hypothesis, must be one of "two.sided" (default), "greater" or "less". You can specify just the initial letter.} 16 | 17 | \item{EXACT}{whether to use exact method.} 18 | } 19 | \value{ 20 | A list with class "htest". 21 | } 22 | \description{ 23 | Sign test. 24 | } 25 | \examples{ 26 | \dontrun{ 27 | sign.test(rnorm(100)) 28 | } 29 | } 30 | \author{ 31 | Jian Li <\email{rinds.book@gmail.com}> 32 | } 33 | 34 | -------------------------------------------------------------------------------- /R/biOps2Image.R: -------------------------------------------------------------------------------- 1 | 2 | ##' Convert imagedata object to Image object. 3 | ##' 4 | ##' @title Convert imagedata object of biOps package to Image object of EBImage package. 5 | ##' @param x imagedata object of biOps package. 6 | ##' @return Image object. 7 | ##' @author Jian Li <\email{rinds.book@@gmail.com}> 8 | ##' @examples \dontrun{ 9 | ##' biOps2Image(x) 10 | ##' } 11 | ##' 12 | 13 | biOps2Image <- function(x) { 14 | if (!require(EBImage)) { 15 | stop("EBImage should be installed!") 16 | } 17 | if (!inherits(x, "imagedata")) { 18 | stop("x should be biOps object!") 19 | } 20 | 21 | colormode <- attributes(x)$type 22 | if (colormode == "rgb") { 23 | colormode <- "Color" 24 | bidata <- x[ , ,]/256 25 | outdata <- array(0, dim = dim(bidata)[c(2, 1, 3)]) 26 | outdata[, , 1] <- t(bidata[, , 1] ) 27 | outdata[, , 2] <- t(bidata[, , 2] ) 28 | outdata[, , 3] <- t(bidata[, , 3] ) 29 | } 30 | if (colormode == "grey") { 31 | colormode <- "Grayscale" 32 | bidata <- x[ ,]/256 33 | outdata <- t(bidata) 34 | } 35 | 36 | return(Image(data = outdata, colormode = colormode)) 37 | } 38 | 39 | -------------------------------------------------------------------------------- /R/Image2biOps.R: -------------------------------------------------------------------------------- 1 | 2 | ##' Convert Image object to imagedata object. 3 | ##' 4 | ##' @title Convert Image object of EBImage package to imagedata object of biOps package. 5 | ##' @param x Image object of EBImage package. 6 | ##' @return imagedata object. 7 | ##' @author Jian Li <\email{rinds.book@@gmail.com}> 8 | ##' @examples \dontrun{ 9 | ##' Image2biOps(x) 10 | ##' } 11 | ##' 12 | 13 | Image2biOps <- function(x) { 14 | if (!require(biOps)) { 15 | stop("biOps should be installed!") 16 | } 17 | if (!inherits(x, "Image")) { 18 | stop("x should be Image object!") 19 | } 20 | 21 | ebdata <- x@.Data * 256 22 | if (x@colormode == "Grayscale" || x@colormode == 0) ebtype <- "grey" 23 | if (x@colormode == "Color" || x@colormode == 2) ebtype <- "rgb" 24 | 25 | if (ebtype == "rgb") { 26 | outdata <- array(0, dim = dim(ebdata)[c(2, 1, 3)]) 27 | outdata[, , 1] <- t(ebdata[, , 1] ) 28 | outdata[, , 2] <- t(ebdata[, , 2] ) 29 | outdata[, , 3] <- t(ebdata[, , 3] ) 30 | } 31 | if (ebtype == "grey") { 32 | outdata <- t(ebdata) 33 | } 34 | 35 | return(imagedata(outdata, type = ebtype)) 36 | } 37 | 38 | -------------------------------------------------------------------------------- /R/sign.test.R: -------------------------------------------------------------------------------- 1 | 2 | ##' Sign test. 3 | ##' 4 | ##' @title Sign test. 5 | ##' @param v numeric vector of data values. Non-finite (e.g. infinite or missing) values will be omitted. 6 | ##' @param m0 a number specifying an optional parameter used to form the null hypothesis. 7 | ##' @param h1 a character string specifying the alternative hypothesis, must be one of "two.sided" (default), "greater" or "less". You can specify just the initial letter. 8 | ##' @param EXACT whether to use exact method. 9 | ##' @return A list with class "htest". 10 | ##' @author Jian Li <\email{rinds.book@@gmail.com}> 11 | ##' @examples \dontrun{ 12 | ##' sign.test(rnorm(100)) 13 | ##' } 14 | ##' 15 | 16 | sign.test <- function(v,m0,h1=c("two.sided","less","greater"),EXACT=c("exact","approximation")) 17 | {h1<-match.arg(h1) 18 | EXACT<-match.arg(EXACT) 19 | DNAME<-paste(deparse(substitute(v)),"and m0=",deparse(substitute(m0))) 20 | v<-v[!is.na(v)] 21 | if(!is.numeric(v)){stop("v must be numeric")} 22 | if(!is.numeric(m0)){stop("m0 must be numeric")} 23 | if(length(v)<1){stop("Not enough v data")} 24 | s1<-length(v[vm0]);n<-s1+s2 25 | st<-switch(h1,two.sided=min(s1,s2),less=s2,greater=s1) 26 | if(EXACT=="exact") 27 | {pv<-switch(h1,two.sided=min(2*pbinom(min(s1,s2),n,0.5),1),less=pbinom(s2,n,0.5),greater=pbinom(s1,n,0.5)) 28 | METHOD<-"sign test (exact)" 29 | names(st)<-switch(h1,two.sided="S",less="S+",greater="S-")} 30 | else{st<-(st+0.5*(-1)^(as.integer(st>n/2))-n/2)*2/n^0.5 31 | pv<-switch(h1,two.sided=min(2*pnorm(st),1),less=pnorm(st),greater=pnorm(st)) 32 | METHOD<-"sign test (approximation)" 33 | names(st)<-"Z"} 34 | output<-list(statistic=st, p.value=pv, alternative=h1, method=METHOD, data.name=DNAME) 35 | class(output)<-"htest";return(output) 36 | } 37 | 38 | -------------------------------------------------------------------------------- /R/emax.fit.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | emax.fit <- function (y, dose, iparm = c("ed50", "e0", "emax"), ed50cutoff = 2.5 * max(doselev)) 4 | { 5 | dm <- tapply(y, dose, mean) 6 | dsd <- sqrt(tapply(y, dose, var)) 7 | intercept <- rep(1, length(dose)) 8 | doselev <- sort(unique(dose)) 9 | est3 <- rep(NA, 3) 10 | estA <- rep(NA, 2) 11 | 12 | e0 <- min(dm) 13 | emax <- max(dm) - e0 14 | ed50 <- max(doselev)/2 15 | Sparm <- c(ed50 = ed50, e0 = e0, emax = emax) 16 | 17 | fit <- try(nls(y ~ e0 + (emax * dose)/(dose + ed50), 18 | start = Sparm, control = nls.control(maxiter = 100), 19 | trace = F), silent = T) 20 | 21 | AcceptConv <- F 22 | if (class(fit) == "nls") { 23 | est3 <- coef(fit) 24 | if (coef(fit)[1] > 0) { 25 | if (coef(fit)[1] <= ed50cutoff) { 26 | vc <- as.vector(vcov(fit)) 27 | seout <- SeEmax3(fit, doselev) 28 | fitpred <- seout$fitpred 29 | sdpred <- seout$sdpred 30 | sddif <- seout$sddif 31 | AcceptConv <- T 32 | } 33 | } 34 | } 35 | if (!AcceptConv) { 36 | fitL <- lm(y ~ dose) 37 | sigL <- summary(fitL)$sigma 38 | predobj <- predict(fitL, data.frame(dose = doselev), se.fit = T) 39 | fitpred <- predobj$fit 40 | sdpred <- predobj$se.fit 41 | sddif <- doselev * sqrt(vcov(fitL)[2, 2]) 42 | } 43 | return(list(fitpred = fitpred, 44 | sdpred = sdpred, sddif = sddif)) 45 | } 46 | 47 | SeEmax3<-function (fit, doselev) 48 | { 49 | E0<-coef(fit)[2] 50 | ED50<-coef(fit)[1] 51 | EMAX<-coef(fit)[3] 52 | fitpred <- E0+(EMAX*doselev)/(ED50+doselev) 53 | fitdif <- fitpred - E0 54 | vfcov <- vcov(fit) 55 | sddif <- sqrt((fitdif^2) * (vfcov[3, 3]/coef(fit)[3]^2 + 56 | vfcov[1, 1]/(coef(fit)[1] + doselev)^2 - (2 * vfcov[1, 57 | 3])/(coef(fit)[3] * (coef(fit)[1] + doselev)))) 58 | covint <- -((doselev * coef(fit)[3])/(coef(fit)[1] + doselev)^2) * 59 | vfcov[2, 1] + (doselev/(coef(fit)[1] + doselev)) * vfcov[2, 60 | 3] 61 | sdpred <- sqrt(vfcov[2, 2] + sddif^2 + 2 * covint) 62 | return(list(fitpred = fitpred, sdpred = sdpred, fitdif = fitdif, 63 | sddif = sddif)) 64 | } 65 | 66 | 67 | -------------------------------------------------------------------------------- /R/plotTS.R: -------------------------------------------------------------------------------- 1 | 2 | ##' Plot time series and its forecast values. 3 | ##' 4 | ##' @title Plot time series and its forecast values. 5 | ##' @param series ts object. 6 | ##' @param forecast the output of predict or a forecast object. 7 | ##' @return plot. 8 | ##' @author Jian Li <\email{rinds.book@@gmail.com}> 9 | ##' 10 | plotTS <- function(series, forecast) { 11 | if (inherits(forecast, "forecast")) { 12 | forecast <- as.ts(forecast) 13 | forecast$pred <- forecast$mean 14 | if ("95%" %in% forecast$upper) { 15 | forecast$se <- (forecast$upper[, "95%"] - forecast$mean)/1.96 16 | } else { 17 | forecast$se <- (forecast$upper[, 1] - forecast$mean)/1.96 18 | } 19 | } 20 | if (length(forecast) > 0) { 21 | act.start <- attributes(series)$tsp[1] 22 | act.end <- attributes(series)$tsp[2] 23 | pred.start <- attributes(forecast$pred)$tsp[1] 24 | pred.end <- attributes(forecast$pred)$tsp[2] 25 | 26 | ylim <- c( min(series,forecast$pred - 1.96 * forecast$se), max(series,forecast$pred + 1.96 * forecast$se)) 27 | xlim <- c(act.start, pred.end) 28 | 29 | #opar <- par(mar=c(4,4,2,2),las=1) 30 | opar <- par(mar=c(4,4,0.5,2)) 31 | plot(series, ylim = ylim, type = "n", xlim = xlim) 32 | usr <- par("usr") 33 | 34 | rect(usr[1], usr[3], pred.start ,usr[4],border=NA,col="lemonchiffon") 35 | rect(pred.start ,usr[3], usr[2], usr[4],border=NA,col="lavender") 36 | abline(h= seq(from = ylim[1], to = ylim[2], length.out = 10) , col ="gray" , lty =3) 37 | 38 | polygon( c(as.vector(time(forecast$pred)), rev(as.vector(time(forecast$pred)))), 39 | c(forecast$pred - 1.96*forecast$se,rev(forecast$pred + 1.96*forecast$se)), 40 | col = "orange", 41 | lty=2,border=NA) 42 | 43 | lines( as.vector(time(forecast$pred)) , forecast$pred - 1.96*forecast$se , lty=2) 44 | lines( as.vector(time(forecast$pred)) , forecast$pred + 1.96*forecast$se , lty=2) 45 | 46 | lines( series , lwd=2 ) 47 | lines( forecast$pred , lwd=2 ) 48 | lines( as.vector(time(forecast$pred)) , forecast$pred , lwd=2 , col ="white") 49 | 50 | legend( "topleft", inset=0, legend = c("series","prediction","95% confidence band"), 51 | fill=c("black","white","orange"), bg = "gray") 52 | 53 | box() 54 | par(opar) 55 | } else { 56 | ylim <- c( min(series), max(series)) 57 | opar <- par(mar=c(4,4,0.5,2)) 58 | plot(series, type = "n", ylim = ylim) 59 | usr <- par("usr") 60 | abline(h= seq(from = ylim[1], to = ylim[2], length.out = 10) , col ="gray" , lty =3) 61 | lines( series , lwd=2 ) 62 | legend( "topleft", inset=0, legend = c("series"), fill=c("black"), bg = "gray") 63 | box() 64 | par(opar) 65 | } 66 | 67 | } 68 | 69 | 70 | -------------------------------------------------------------------------------- /inst/examples/optimization/sudoku.mod: -------------------------------------------------------------------------------- 1 | /* SUDOKU, Number Placement Puzzle */ 2 | 3 | /* Written in GNU MathProg by Andrew Makhorin */ 4 | 5 | /* Sudoku, also known as Number Place, is a logic-based placement 6 | puzzle. The aim of the canonical puzzle is to enter a numerical 7 | digit from 1 through 9 in each cell of a 9x9 grid made up of 3x3 8 | subgrids (called "regions"), starting with various digits given in 9 | some cells (the "givens"). Each row, column, and region must contain 10 | only one instance of each numeral. 11 | 12 | Example: 13 | 14 | +-------+-------+-------+ 15 | | 5 3 . | . 7 . | . . . | 16 | | 6 . . | 1 9 5 | . . . | 17 | | . 9 8 | . . . | . 6 . | 18 | +-------+-------+-------+ 19 | | 8 . . | . 6 . | . . 3 | 20 | | 4 . . | 8 . 3 | . . 1 | 21 | | 7 . . | . 2 . | . . 6 | 22 | +-------+-------+-------+ 23 | | . 6 . | . . . | 2 8 . | 24 | | . . . | 4 1 9 | . . 5 | 25 | | . . . | . 8 . | . 7 9 | 26 | +-------+-------+-------+ 27 | 28 | (From Wikipedia, the free encyclopedia.) */ 29 | 30 | param givens{1..9, 1..9}, integer, >= 0, <= 9, default 0; 31 | /* the "givens" */ 32 | 33 | var x{i in 1..9, j in 1..9, k in 1..9}, binary; 34 | /* x[i,j,k] = 1 means cell [i,j] is assigned number k */ 35 | 36 | s.t. fa{i in 1..9, j in 1..9, k in 1..9: givens[i,j] != 0}: 37 | x[i,j,k] = (if givens[i,j] = k then 1 else 0); 38 | /* assign pre-defined numbers using the "givens" */ 39 | 40 | s.t. fb{i in 1..9, j in 1..9}: sum{k in 1..9} x[i,j,k] = 1; 41 | /* each cell must be assigned exactly one number */ 42 | 43 | s.t. fc{i in 1..9, k in 1..9}: sum{j in 1..9} x[i,j,k] = 1; 44 | /* cells in the same row must be assigned distinct numbers */ 45 | 46 | s.t. fd{j in 1..9, k in 1..9}: sum{i in 1..9} x[i,j,k] = 1; 47 | /* cells in the same column must be assigned distinct numbers */ 48 | 49 | s.t. fe{I in 1..9 by 3, J in 1..9 by 3, k in 1..9}: 50 | sum{i in I..I+2, j in J..J+2} x[i,j,k] = 1; 51 | /* cells in the same region must be assigned distinct numbers */ 52 | 53 | /* there is no need for an objective function here */ 54 | 55 | solve; 56 | 57 | for {i in 1..9} 58 | { for {0..0: i = 1 or i = 4 or i = 7} 59 | printf " +-------+-------+-------+\n"; 60 | for {j in 1..9} 61 | { for {0..0: j = 1 or j = 4 or j = 7} printf(" |"); 62 | printf " %d", sum{k in 1..9} x[i,j,k] * k; 63 | for {0..0: j = 9} printf(" |\n"); 64 | } 65 | for {0..0: i = 9} 66 | printf " +-------+-------+-------+\n"; 67 | } 68 | 69 | data; 70 | 71 | /* These data correspond to the example above. */ 72 | 73 | param givens : 1 2 3 4 5 6 7 8 9 := 74 | 1 5 3 . . 7 . . . . 75 | 2 6 . . 1 9 5 . . . 76 | 3 . 9 8 . . . . 6 . 77 | 4 8 . . . 6 . . . 3 78 | 5 4 . . 8 . 3 . . 1 79 | 6 7 . . . 2 . . . 6 80 | 7 . 6 . . . . 2 8 . 81 | 8 . . . 4 1 9 . . 5 82 | 9 . . . . 8 . . 7 9 ; 83 | 84 | end; 85 | -------------------------------------------------------------------------------- /R/installAllDSPkgs.R: -------------------------------------------------------------------------------- 1 | 2 | ##' Load all the required packages mentioned in this book. 3 | ##' 4 | ##' @title Load all the required packages mentioned in this book. 5 | ##' @param install Whether to install the uninstalled packages, default is FALSE. 6 | ##' @param lib.loc character vector giving the library directories where to install the packages. Defaults to the first element of \code{\link{.libPaths}()}. 7 | ##' @return TRUE or FALSE. 8 | ##' @author Jian Li <\email{rinds.book@@gmail.com}> 9 | ##' @keywords string 10 | ##' @examples \dontrun{ 11 | ##' loadAllDSPkgs(install = TRUE) 12 | ##' } 13 | ##' 14 | 15 | loadAllDSPkgs <- function(install = FALSE, lib.loc = .libPaths()[1]) 16 | { 17 | .installPkg("roxygen2", lib.loc = lib.loc, install = install) 18 | .installPkg("tmcn", lib.loc = lib.loc, install = install, repos="http://R-Forge.R-project.org") 19 | .installPkg("knitr", lib.loc = lib.loc, install = install) 20 | 21 | .installPkg("reshape2", lib.loc = lib.loc, install = install) 22 | .installPkg("plyr", lib.loc = lib.loc, install = install) 23 | .installPkg("xts", lib.loc = lib.loc, install = install) 24 | .installPkg("quantmod", lib.loc = lib.loc, install = install) 25 | 26 | .installPkg("lmtest", lib.loc = lib.loc, install = install) 27 | .installPkg("car", lib.loc = lib.loc, install = install) 28 | .installPkg("ggplot2", lib.loc = lib.loc, install = install) 29 | 30 | .installPkg("mvtnorm", lib.loc = lib.loc, install = install) 31 | .installPkg("FactoMineR", lib.loc = lib.loc, install = install) 32 | .installPkg("corrplot", lib.loc = lib.loc, install = install) 33 | .installPkg("tseries", lib.loc = lib.loc, install = install) 34 | .installPkg("forecast", lib.loc = lib.loc, install = install) 35 | 36 | .installPkg("fastcluster", lib.loc = lib.loc, install = install) 37 | .installPkg("proxy", lib.loc = lib.loc, install = install) 38 | .installPkg("fpc", lib.loc = lib.loc, install = install) 39 | .installPkg("kohonen", lib.loc = lib.loc, install = install) 40 | .installPkg("caret", lib.loc = lib.loc, install = install) 41 | .installPkg("rpart.plot", lib.loc = lib.loc, install = install) 42 | .installPkg("klaR", lib.loc = lib.loc, install = install) 43 | .installPkg("kernlab", lib.loc = lib.loc, install = install) 44 | .installPkg("mlbench", lib.loc = lib.loc, install = install) 45 | .installPkg("ipred", lib.loc = lib.loc, install = install) 46 | .installPkg("e1071", lib.loc = lib.loc, install = install) 47 | .installPkg("randomForest", lib.loc = lib.loc, install = install) 48 | 49 | .installPkg("lpSolve", lib.loc = lib.loc, install = install) 50 | .installPkg("Rglpk", lib.loc = lib.loc, install = install) 51 | .installPkg("alabama", lib.loc = lib.loc, install = install) 52 | .installPkg("Rsolnp", lib.loc = lib.loc, install = install) 53 | 54 | .installPkg("ggthemes", lib.loc = lib.loc, install = install) 55 | .installPkg("vcd", lib.loc = lib.loc, install = install) 56 | .installPkg("treemap", lib.loc = lib.loc, install = install) 57 | .installPkg("openair", lib.loc = lib.loc, install = install) 58 | 59 | .installPkg("fImport", lib.loc = lib.loc, install = install) 60 | .installPkg("PerformanceAnalytics", lib.loc = lib.loc, install = install) 61 | .installPkg("Quandl", lib.loc = lib.loc, install = install) 62 | 63 | .installPkg("rjson", lib.loc = lib.loc, install = install) 64 | .installPkg("XML", lib.loc = lib.loc, install = install) 65 | .installPkg("rJava", lib.loc = lib.loc, install = install) 66 | .installPkg("tm", lib.loc = lib.loc, install = install) 67 | .installPkg("wordcloud", lib.loc = lib.loc, install = install) 68 | .installPkg("topicmodels", lib.loc = lib.loc, install = install) 69 | .installPkg("Rweibo", lib.loc = lib.loc, install = install, repos="http://R-Forge.R-project.org") 70 | .installPkg("Rwordseg", lib.loc = lib.loc, install = install, repos="http://R-Forge.R-project.org") 71 | 72 | .installPkg("sp", lib.loc = lib.loc, install = install) 73 | .installPkg("maptools", lib.loc = lib.loc, install = install) 74 | .installPkg("plotKML", lib.loc = lib.loc, install = install) 75 | .installPkg("ggmap", lib.loc = lib.loc, install = install) 76 | .installPkg("igraph", lib.loc = lib.loc, install = install) 77 | 78 | .installPkg("RSQLite", lib.loc = lib.loc, install = install) 79 | .installPkg("RODBC", lib.loc = lib.loc, install = install) 80 | 81 | .installPkg("doParallel", lib.loc = lib.loc, install = install) 82 | 83 | } 84 | 85 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | 167 | --------------------------------------------------------------------------------