Thursday, March 14, 2013

My R cheatsheet

1.

library(RODBC)
Channel = odbcDriverConnect("driver={SQL Server};
                            server=server_name;
                            database=database_name")
data = sqlQuery(Channel,
                paste("
SQL query
                       ")
)
sqlSave(Channel, data, tablename="dbo.save_table_from_R")
####################################################################
2.

setwd("C:/Temp")
for(index in 1:10){
  str.index = toString(index)
  title.index = paste("Simulation", str.index)
  png(file = paste(str.index,".png", sep = ""),
      width = 1000, height = 1000,
      bg = "white")
  # par(mfrow=c(2,2))
  # figure 1 to 4
   dev.off()
  cat(index, data1, data2, ...., "\n",
      file="summary.txt", append=TRUE)
}
########################################################################
3.

logit = glm(y ~, family=binomial("logit"),data=,na.action=na.exclude)
y.hat = predict(logit,type = c("response"))
########################################################################
4.

library(pROC)
roc(Y,Y.hat,plot=T)
cut.yhat = cut(y.hat
              ,breaks = quantile(y.hat, probs = seq(0, 1, 1/50))
              ,include.lowest = T
              )
obs = xtabs(cbind(1 - y, y, 1) ~ cut.yhat)
expect = xtabs(cbind(1 - y.hat, y.hat) ~ cut.yhat)
result = xtabs(cbind(y, y.hat,1) ~ cut.yhat)
#############################################################################
5.

splitdf <- function(dataframe, seed=NULL) {
  if (!is.null(seed)) set.seed(seed)
  index <- 1:nrow(dataframe)
  trainindex <- sample(index, trunc(length(index)*0.7))
  trainset <- dataframe[trainindex, ]
  testset <- dataframe[-trainindex, ]
  list(trainset=trainset,testset=testset)
}
data.all = splitdf(data, seed=100)
data.train = data.all$trainset
data.test = data.all$testset




No comments:

Post a Comment