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.

Thursday, August 26, 2010

Django - Deploy an application to alwaysdata.com (French web site)

www.alwaysdata.com is a French web site which allows free django hosting (and others) for websites up to 10Mb.
The site includes a fantastic but in French documentation which explains step by step how to publish a django site:
http://wiki.alwaysdata.com/wiki/D%C3%A9ployer_une_application_Django

Remarks:
  • ssh and ftp connections are possible.
  • There is an admin site https://admin.alwaysdata.com/ from where we need to enable the ssh user before using it.
  • The database must be created from the admin site.
  • When we make changes we may need to restart the fcgi process. This must be done from the admin web site, with the processes option, by killing the process. Immediately it is restarted.
  • Notice, as it is explained in the instructions, that we create a public directory. Some content, specially the static files, must be in that directory to be served, although the development server does not use them, it uses the files in the typical directory.
  • Important! Don't forget to redirect all queries to the main domain to the specific folder /appname/public where the project relies. This must be done from the admin panel.
  • Instructions for working with the development server are in http://wiki.alwaysdata.com/wiki/Utiliser_le_serveur_de_d%C3%A9veloppement_int%C3%A9gr%C3%A9_de_Django

HTML: How do we include one HTML file into another?

Here it is explained:
http://www.boutell.com/newfaq/creating/include.html

It depends on the way we do it: server-side, client-side, etc.
The way I used was by client side (I needed to add the page to a blogger page).
The code I used worked fine!

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, August 18, 2010

Django - Deploy an application to djangoeurope.com

I followed these steps to deploy a Django application into djangoeurope.com:
  • Register to djangoeurope.com and enter to the Panel (panel.djangoeurope.com)
  • Click into the installdjango option and create a project (it is called application in the webpage).
  • Run python manage.py startapp appname (the application should be called equal to our project's application).
  • Access with ssh and sftp to our space and put our original application into the corresponding folder. Overwrite all files except settings.py and settings_development.py.
  • Merge the files settings.py from djangoeurope.com and the one of our application.
  • Copy the content of settings.py to settings_development.py.
  • Run python manage.py syncdb.
  • Modify file lighttpd/django.conf to be correct.
  • Syncronize with init/appname restart, init/lightppd reload and init/lightppd restart.
Useful utilities when working are the commands
zip -r file.zip directory
,
which compresses the directory into the file, and
unzip file.zip,
which uncompresses the directory.
Caution: if we are in folder a/ and we execute unzip b/file.zip, the content of file.zip will be extracted in our working directory a/!