Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Wednesday, February 8, 2012

RMySQL - How to connect R with MySQL in Windows

Following the instructions of http://stackoverflow.com/questions/4785933/adding-rmysql-package-to-r-fails

Important note 1: The installation works when using MySQL for 32bit computers and R for 32bit computers. I have tried to install it to the 64bit version of R but I did not succeed.
  • Install RTools
  • Install MySQL or header and library files of mysql
  • Create or edit file C:\Program Files\R\R-2.12.1\etc\Renviron.site and add two lines like CYGWIN=nodosfilewarning
    MYSQL_HOME=C:/ARCHIV~1/MYSQL/MYSQLS~1.5
  • Copy libmysql.lib from mysql/lib to mysql/lib/opt to meet dependencies.
  • Copy libmysql.dll to C:\Program Files\R\R-2.12.1\bin or to windows/system32 directory.
  • Run install.packages('RMySQL',type='source') and wait while compilation will end.
Important note 2: My computer contains Spanish language Windows, and I had several problems with the accent in the folder named "Configuración local", where the downloaded packages are stored and compiled.
I solved the problem in the following way. I changed the environment variable TMP to another local folder without containing any accent.
After the installation I rolled back the change in the environment variable.

Sunday, September 5, 2010

R - Time Series

Time Series analysis is an important part of statistics.
Bibliography
  • Time Series Analysis and Its Applications: With R Examples, by Robert H. Shumway and David S. Stoffer. In this page http://www.stat.pitt.edu/stoffer/tsa2/index.html there is the source code of the book, which is useful even if we have not read the book.
  • Introduction to Time Series and Forecasting, by Peter J. Brockwell and Richard A. Davis.
R webpage:

Sunday, August 29, 2010

R - Vectors, Lists, and DataFrames

  • A vector can be thought as contiguous cells containing elemental data.
Creation of vectors: x <- c(1,2,3)
Indexing: x[i], x[i,j]
Concatenating vectors x <- c(x,x)

  • A list (generic vector) has elements, each of which can contain any type of R object.
Creation of lists: x <- list(name_1=object_1, ..., name_m=object_m)
Indexing: x[[i]], x[[i,j]], x$a, x$"a", x[["a"]]
Concatenating lists: list.ABC <- c(list.A, list.B, list.C)
  • A data frame is a list with class "data.frame". There are restrictions:
    • The components must be vectors (numeric, character, or logical), factors, numeric matrices, lists, or other data frames.
    • Matrices, lists, and data frames provide as many variables to the new data frame as they have columns, elements, or variables, respectively.
    • Numeric vectors, logicals and factors are included as is, and character vectors are coerced to be factors, whose levels are the unique values appearing in the vector.
    • Vector structures appearing as variables of the data frame must all have the same length, and matrix structures must all have the same row size.
Creation of dataframes: accountants <- data.frame(home=statef, loot=incomes, shot=incomef)
Attach and dettach: (For lists in general). A useful facility would be somehow to make the components of a list or data frame temporarily visible as variables under their component name, without the need to quote the list name explicitly each time.The attach() function takes a `database' such as a list or data frame as its argument.

Saturday, August 21, 2010

R - Fitting data using Linear Models

Linear Model explanation
http://stat.ethz.ch/R-manual/R-devel/library/stats/html/lm.html

Formula object explanation
http://stat.ethz.ch/R-manual/R-devel/library/stats/html/formula.html

Example

x <- seq(0, 10, 0.1)

y <- 1 + x + cos(x) + rnorm(x)
yy <- 1 + x + cos(x)

plot(x, y, lwd = 3)
lines(x, yy, lwd = 3)

# 1 (default) includes intercept - 0 no
fit <- lm(y ~ 1 + x + I(x^2) + I(cos(x)))
fit
summary(fit)

newdf <- data.frame(x <- seq(11,2000,0.1))
newdf <- data.frame(newdf,y <- 1 + newdf$x + cos(newdf$x) + rnorm(newdf$x))
newp <- predict(fit,newdf)

plot(newdf$x,newdf$y,lwd=3)
lines(newdf$x,newp,lwd=3)

Wednesday, July 21, 2010

R - Problema al carregar una llibreria

M'he trobat amb el següent problema
  • Amb un usuari vaig instal·lar una llibreria.
  • Des d'un altre usuari (l'usuari apache www-data) volia carregar la llibreria però no podia.
  • Accedint com a super usuari em carregava la llibreria correctament.
Explicació:
  • Hi ha dos tipus de repositoris on s'instal·len les llibreries: als directoris locals dels usuaris i en global per a tots
  • Per defecte s'instal·len les llibraries als directoris locals dels usuaris
  • Només quan l'usuari no té /home és quan les llibreries s'instal·len en global.
  • Per a canviar d'usuari, si no en sabem el password, podem accedir primer al super usuari i després canviar a l'usuari en que volem: sudo su / su usuari2

Friday, June 11, 2010

R - Com fer un gràfic senzill

# Resultat en un fitxer pdf. width=7, height=7 by default. (in inches)
pdf(file="prova.pdf",width=7,height=7)

# Parametres dels grafics. mfrow es per a posar mes d'un grafic a la figura. Cada grafic comença amb plot()

# Els altres son per als marges
#par(mfrow=c(3,3), mar=c(3.5,3.5,1.5,0.5), oma=c(0,1,1,1))

a <- matrix(ncol=2,nrow=10) a[,1] = c(1,2,3,4,5,6,7,8,9,10) a[,2] = c(3,5,3,87,9,63,12,51,11,22) b <- matrix(ncol=2,nrow=10) b[,1] = c(1,2,3,4,5,6,7,8,9,10) b[,2] = c(13,15,23,17,59,3,27,75,18,32) plot(a,col="green",pch=17,ylim=c(-1,100),xlab="alpha",ylab="Variation of Information",main="sites.net"); lines(a,col="green") points(b,col="red",pch=20); lines(b,col="red") legend("topright",c("a","b"),col=c("green", "red"),pch=c(17,20)); # Tanquem el fitxer pdf dev.off()


El resultat és

R - Utilitats

El llenguatge R és un llenguatge matemàtic molt útil. En destaco un parell de funcionalitats:
  • La funció match(), que retorna els índexos dels elements buscats.
  • par(mfrow=c(3,3)), que permet mostrar 3x3 gràfics en una sola finestra.
  • pdf(...,width=7,length=7), que permet imprimir a un fitxer pdf els gràfics. Acaba amd dev.off()
  • mca, que fa un anàlisi de correspondències múltiples. Dins la library MASS
  • hclust, que fa un hierarchical clustering
  • hist2d, image, image.plot, que són eines gràfiques
  • choose(n,m) calcula combinacions de n elements en grups de n
  • ginv(M) calcula la inversa de M. Dins la library MASS
  • binary(i,d) obté la representació en binari de d digits del nombre i. Dins la library wle. binary(i,d)$binary és un vector de zeros i uns.
  • write.table(b,file="filedest", row.names=FALSE, col.names=FALSE)
  • plot(a,col="green",pch=17,ylim=c(-1,100),xlab="alpha", ylab="Information", main="net")
  • Creacio de funcions: nomfunc <- function(parameters){... retValue}