## Friday, August 23, 2013

### Source Code Management Using Git

Hello everyone!
In this article, I want to talk about version control systems. Actually management source code using Git. We use Git on the projects in which multiple participants. Git is not the only way to use version control. There are others which are so popular; SVN, Mercurial and CVS.

In this article, we will continue using code.google.com server. You can also get started to create an account from there. In code.google.com server, there are SVN, Mercurial and Git options. So, before the processing clone your project to local machine, we define username and password to log in. Sure, you have to set Git software up first.

echo machine code.google.com >> ~/.netrc
chmod go= ~/.netrc


touch test.inc
git commit -m "test file has been added"
git push myproject master:master


After doing this, test.inc file has been added successfuly. Following the first push, inserting, deleting and updating will be made.

touch text.txt
git status
git rm test.inc
git status -s
git commit -m "some files has been changed"
git push


The commands given above, we can see touching and text file easily. test.inc file has been removed and created text.txt file, and logged "some files has been changed" message. After all, we push all change

git pull


Take care!

## Tuesday, August 20, 2013

### Redirect Mobile Devices Using PHP

Hi everyone!

If you have your own web site or something else, It means that you have members, users or followers on your site. Sure, we should remember that most of people uses mobile devices. Internet, social media, game or some applications etc, and we know that they visit your web site from their mobile devices. For that reason if we put this special feature on, we get more quality site of course.
As I said the title, I am doing this using PHP. So, let me show you:
//just define user's agents (iphone or android)
$iphoneDevice = strpos($_SERVER['HTTP_USER_AGENT'], "iPhone");
$androidDevice = strpos($_SERVER['HTTP_USER_AGENT'], "Android");

if($iphoneDevice) { print "Your device is iphone!"; } if($androidDevice) {
print "
FAST BOY";
}


That's it. If the user use iphone, will see "Your device is iphone!" message on the screen, if use android device,
Your device is android, you can download Fast Boy Game
FAST BOY (clickable)


If you want to put more features on your web site, for example blackberry, webos or ipod, you can use already.
$webOSDevice = strpos($_SERVER['HTTP_USER_AGENT'], "webOS");
$BlackBerryDevice = strpos($_SERVER['HTTP_USER_AGENT'], "BlackBerry");
$ipodDevice = strpos($_SERVER['HTTP_USER_AGENT'], "iPod");

The code given above shows us other devices and brands.
We will see you next article!

## A gWidget Example - Using windows, groups, labels, text and password boxes, buttons and events in R

August 20, 2013

In this entry, a short example for using gWidgets is given. gWidgets is a package for creating GUI’s in R.

Our example shows a GUI window with width = 400 and height = 400. Window is created by gwindow function. Components are located by rows. Rows are handled by ggroup function. ggroup must take a container as a parameter. In this logic, lbl_username and txt_username are childs of row1 which is child of gwindow.

Any text field can act as a password field by using visible¡- function.

So, the object txt_password is now hiding characters by * characters. Finally, the method addHandlerClicked links an object to a function for click event. In our example, btn_login is linked to do_login function. When btn_login clicked, a message is written. The source code of the complete example is given below.

2require("gWidgets")
3require("gWidgetstcltk")
4
5# main window
7                width=400,
8                height=400)
9
10# a row and components
11row1 <- ggroup(container=main)
14
15
16# a row and components
17row2 <- ggroup(container=main)
20
21# any text in txt_password will be show with * character
23
24# a row for button
25row3 <- ggroup(container=main)
28btn_register <- gbutton(container=row3,
29                        text="Register")
30
31
32# Event handler for login button
35}
36
37# Event handler for register button
38do_register <- function(obj){
40}
41
42
43# Registering Events

## Monday, August 19, 2013

### Greek Laters in LaTeX

Greek letters are essential in writing mathematical equations. Many text editors
provide equation editors but we can say the most comprehensive one is the LaTeX. The list below represents letters and corresponding LaTeX commands.
The image of this list is imported from the site http://web.ift.uib.no/Teori/KURS/WRK/TeX/sym1.html
 http://web.ift.uib.no/Teori/KURS/WRK/TeX/t1.gif

A list of other special characters can be found in site http://www.noao.edu/noaoprop/help/symbols/

### Getting MySQL Backup and Archiving

Hi everyone!

In this article, I am going to show you how to get MySQL backup and archive on Linux. If you use MySQL or any data base, you need backup your database and archive after. So, If you work for huge datas, you get what I mean actually.

I am sure that you heard phpmyadmin before. This is alternative with a user friendly interface for developers. You can import and export datas or all databases. If your size of data is small or medium, phpmyadmin will be perfect for this. But if the size of data increases, phpmyadmin should be doomed. That's why we need console, namely mysqldump! In short if you dont want to get 504 gateway error message, just use it :)

In this regard, let me show you some codes:
$. mysqldump --add-drop-table -h localhost -u username -p dbname > dbname.sql$. ls
dbname.sql


The commands given above show us getting dbname database backup and touching dbname.sql file. Using ls command, just listing files in the folder.
$. tar cvzf archive.tar.gz dbname.sql dbname.sql$. ls
archive.tar.gz dbname.sql


The commands given above also show us archiving dbname.sql file to archive.tar.gz archive file. After that listing files on there.
Like you have seen up here, we created a archive file. But if want to open it, just do like:
$. tar xvzf archive.tar.gz dbname.sql  Then we need remove .sql extension file, because it's size may be huge. $. rm *.sql
$. ls  That's it! What did we do above? We just get the database backup and archive it. After that remove the .sql file. What we've got is my backup archive. We'll see you next article! ### How To Create Scheduled Jobs With Crontab in Linux Hi everyone! In this article, I want to talk about cronjob. Some of us heard about this subject before. Cronjob scheduled task used in the sense of is very important for us. Because you can use it everytime you have to set some scripts will run next date. So, imagine that you have got a web site there are so many users on it. If you want to send e-mail to all your members at 02:00 am, here is the time you need cronjob! Some hosting companies offer to manage cronjob service, but not all. Because of it is every hosting have not CPanel. Maybe you can use SSH or something else. Me, show you how to manage cronjob using SSH. In this regard, open your Unix Server using SSH, and; vi /etc/crontab  * * * * * ( [minute] [hour] [day] [month] [day of the week] ) ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └───── day of the week (0 – 7) (total 7 days) │ │ │ └────────── month (1 – 12) │ │ └─────────────── day (1 – 31) │ └──────────────────── hour (0 – 23) └───────────────────────── minute (0 – 59) //test.php file$db->query("INSERT INTO db_name.table_name(field_name) values('test')");

#cronjob file
* * * * * username php test.php


The code given above shows us the script that adds data to the database every minutes.
*/15 * * * * username php test.php


The code given above also shows us the script that adds data to the database every 15 minutes.
#02:00am at night
00 02 * * * username php test.php

#every 3 hours during the day
0 */3 * * * username php test.php

#each hour
0 * * * * username php test.php

#4 pm o'clock on the 15th day of each month
00 16 15 * * username php test.php

#weeks every night at 2 am
00 02 * * 1-5 username php test.php

#every hour every Sunday
0 * * * sun

#every 3 hours
0 2,5,8,11,14,17,20,23 * * * username php test.php


The codes given above shows us different cronjob examples. You can try and work for creative and usefull cronjob examples.
We'll see you guys next article!

### Linear Regression Revisited

If she loves you more each and every day, by linear regression she hated you before you met.

- Out, liar!

## Sunday, August 18, 2013

### Econometrics Beat: Dave Giles' Blog: Large and Small Regression Coefficients

Econometrics Beat: Dave Giles' Blog: Large and Small Regression Coefficients: Here's a trap that newbies to regression analysis have been known to fall into. It's to do with comparing the numerical values of t...

## Generating LATEXTables in R

August 18, 2013

Many R users prepare reports, documents or research papers using LATEXtype-setting system. Since typing tabular data structures in LATEXby hand consumes too much construction time, some packages were developed for automatic creation or conversion of R datasets. In this short entry, we show the usage and output of xtable function in xtable package. First install the package if it is not already installed:

> install.packages(”xtable”)

After installation, the package is loaded once in the current session:

> require(”xtable”)

Now, suppose that we have a data frame and we want to export it as a LATEXtable. Let’s create a random dataset with three variables, x,y and z.

> x<-round(runif(10,0,100))
> y<-round(runif(10,0,100))
> z<-round(runif(10,0,100))
> data <- as.data.frame ( cbind(x,y,z) )
> data
x   y  z
1  26  86 44
2  81  13 22
3  39  27 57
4  32  57 56
5  50  15 31
6  34 100 98
7  54  46 24
8  42  55 42
9  62  91 77
10  5  73 25

Calling the function xtable simply on the data that we have just created produces some output:

> xtable(data)
% latex table generated in R 2.15.3 by xtable 1.7-1 package
% Sun Aug 18 23:40:23 2013
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
\hline
& x & y & z \\
\hline
1 & 26.00 & 86.00 & 44.00 \\
2 & 81.00 & 13.00 & 22.00 \\
3 & 39.00 & 27.00 & 57.00 \\
4 & 32.00 & 57.00 & 56.00 \\
5 & 50.00 & 15.00 & 31.00 \\
6 & 34.00 & 100.00 & 98.00 \\
7 & 54.00 & 46.00 & 24.00 \\
8 & 42.00 & 55.00 & 42.00 \\
9 & 62.00 & 91.00 & 77.00 \\
10 & 5.00 & 73.00 & 25.00 \\
\hline
\end{tabular}
\end{table}

The generated output can easly be integrated with an LATEXfile. This table is shown as

 x y z 1 26.00 86.00 44.00 2 81.00 13.00 22.00 3 39.00 27.00 57.00 4 32.00 57.00 56.00 5 50.00 15.00 31.00 6 34.00 100.00 98.00 7 54.00 46.00 24.00 8 42.00 55.00 42.00 9 62.00 91.00 77.00 10 5.00 73.00 25.00

This is an example of xtable function call with default parameters. In the next example, we put a caption.

> xtable(data,caption=”Our random dataset”)

 x y z 1 26.00 86.00 44.00 2 81.00 13.00 22.00 3 39.00 27.00 57.00 4 32.00 57.00 56.00 5 50.00 15.00 31.00 6 34.00 100.00 98.00 7 54.00 46.00 24.00 8 42.00 55.00 42.00 9 62.00 91.00 77.00 10 5.00 73.00 25.00

Table 1: Our random dataset

Let’s put a label on it:

> xtable(data,caption=”Our random dataset”,
label=”This is a label”)

 x y z 1 26.00 86.00 44.00 2 81.00 13.00 22.00 3 39.00 27.00 57.00 4 32.00 57.00 56.00 5 50.00 15.00 31.00 6 34.00 100.00 98.00 7 54.00 46.00 24.00 8 42.00 55.00 42.00 9 62.00 91.00 77.00 10 5.00 73.00 25.00

Table 2: Our random dataset

And this function call shows the numbers with three fractional digits.

> xtable(data,caption=”Our random dataset”,
label=”This is a label”,
digits=3)

 x y z 1 26.000 86.000 44.000 2 81.000 13.000 22.000 3 39.000 27.000 57.000 4 32.000 57.000 56.000 5 50.000 15.000 31.000 6 34.000 100.000 98.000 7 54.000 46.000 24.000 8 42.000 55.000 42.000 9 62.000 91.000 77.000 10 5.000 73.000 25.000

Table 3: Our random dataset

## Sorting Multi-Column Datasets in R

August 18, 2013

In this entry, we present the most straightforward way to sort multi-column datasets

Suppose that we have a vector in three dimensional space. This vector can be defined as

<- c(3,1,2)

in R. The built-in function sort sorts a given vector in ascending order by default. An ordered version of vector a is calculated as shown below:

sorted_<- sort(a)

The result is

[1] 1 2 3

For reverse ordering, the value of decreasing parameter must be set to TRUE.

> sort(a,decreasing=TRUE)
[1] 3 2 1

Another related function order returns indices of sorted elements.

> a <- c(3,1,2)
> order (a)
[1] 2 3 1

In the example above, it is shown that if the vector a is ordered in ascending order, second element of a will be placed at index 1. It is easy to show that, result of the function order can be used to sort data.

> a <- c(3,1,2)
> o <- order (a)
> a[o]
[1] 1 2 3

Suppose that we need to sort a matrix by a desired row. The solution is easy. Get the indices of sorted desired column and use the same method as in example given above.

> x <- round(runif(5, 0, 100))
> y <- round(runif(5, 0, 100))
> z <- round(runif(5, 0, 100))
> data <- cbind(x,y,z)
> data
x  y  z
[1,] 48 35 75
[2,] 40 21 43
[3,] 58 69  1
[4,] 49 38  2
[5,] 43 66 46

Now, get the indices of sorted z:

> o <- order(z)
> data[o,]
x  y  z
[1,] 58 69  1
[2,] 49 38  2
[3,] 40 21 43
[4,] 43 66 46
[5,] 48 35 75

Finally, we sorted the dataset data by the column vector z.

## Saturday, August 17, 2013

### A User Document For RCaller

A new research paper as a RCaller documentation is freely available at http://www.sciencedomain.org/abstract.php?iid=550&id=6&aid=4838#.U5YSoPmSy1Y

## RCaller: A library for calling R from Java

August 17, 2013

### Contents

Abstract

RCaller is an open-source, compact, and easy-to-use library for calling R from Java. It offers not only an elegant solution for the task but its simplicity is key for non-programmers or programmers who are not familier with the internal structure of R. Since R is not only a statistical software but an enormous collection of statistical functions, accessing its functions and packages is of tremendous value. In this short paper, we give a brief introduction on the most widely-used methods to call R from Java and highlight some properties of RCaller with short examples. User feedback has shown that RCaller is an important tool in many cases where performance is not a central concern.

### 1 Introduction

R is an open source and freely distributed statistics software package for which hundreds of external packages are available. The core functionality of R is written mostly in C and wrapped by R functions which simplify parameter passing. Since R manages the exhaustive dynamic library loading tasks in a clever way, calling an external compiled function is easy as calling an R function in R. However, integration with JVM (Java Virtual Machine) languages is painful.
The R package rJava provides a useful mechanism for instantiating Java objects, accessing class elements and passing R objects to Java methods in R. This library is convenient for the R packages that rely on external functionality written in Java rather than C, C++ or Fortran.
The library JRI, which is now a part of the package rJava, uses JNI (Java Native Interface) to call R from Java . Although JNI is the most common way of accessing native libraries in Java, JRI requires that several system and environment variables are correctly set before any run, which can be difficult for inexperienced users, especially those who are not computer scientists.
The package Rserve uses TCP sockets and acts as a TCP server. A client establishes a connection to Rserve, sends R commands, and receives the results. This way of calling R from the other platforms is more general because the handshaking and the protocol initializing is fully platform independent.
Renjin (http://code.google.com/p/renjin) is an other interesting project that addresses the problem. It solves the problem of calling R from Java by re-implementing the R interpreter in Java! With this definition, the project includes the tasks of writing the interpreter and implementing the internals. Renjin is intended to be 100% compatible with the original. However, it is under development and needs help. After all, an online demo is available which is updated simultaneously when the source code is updated.
Finally, RCaller is an LGPL’d library which is very easy to use. It does not do much but wraps the operations well. It requires no configuration beyond installing an R package (Runiversal) and locating the Rscript binary distributed with R. Altough it is known to be relatively inefficient compared to other options, its latest release features significant performance improvements.

### 2 Calling R Functions

Calling R code from other languages is not trivial. R includes a huge collection of math and statistics libraries with nearly 700 internal functions and hundreds of external packages. No comparable library exists in Java. Although libraries such as the Apache Commons Math do provide many classes for those calculations, its scope is quite limited compared to R. For example, it is not easy to find such a library that calculates quantiles and probabilities of non-central distributions. affirms that using R’s functionality from Java prevents the user from writing duplicative codes in statistics softwares.
RCaller is an other open source library for performing R operations from within Java applications in a wrapped way. RCaller prepares R code using the user input. The user input is generally a Java array, a plain Java object or the R code itself. It then creates an external R process by running the Rscript executable. It passes the generated R code and receives the output as XML documents. While the process is alive, the output of the standard input and the standard error streams are handled by an event-driven mechanism. The returned XML document is then parsed and the returned R objects are extracted to Java arrays.
The short example given below creates two double vectors, passes them to R, and returns the residuals calculated from a linear regression estimation.
RCaller caller = new RCaller();
RCode code = new RCode();
double[] xvector = new double[]{1,3,5,3,2,4};
double[] yvector = new double[]{6,7,5,6,5,6};

caller.setRscriptExecutable("/usr/bin/Rscript");

code.addRCode("ols <- lm ( Y ~ X )");

caller.setRCode(code);

caller.runAndReturnResult("ols");

double[] residuals =
caller.getParser().
getAsDoubleArray("residuals");

The lm function returns an R list with a class of lm whose elements are accessible with the \$ operator. The method runAndReturnResult() takes the name of an R list which contains the desired results. Finally, the method getAsDoubleArray() returns a double vector with values filled from the vector residuals of the list ols.
RCaller uses the R package Runiversal to convert R lists to XML documents within the R process. This package includes the method makexml() which takes an R list as input and returns a string of XML document. Although some R functions return the results in other types and classes of data, those results can be returned to the JVM indirectly. Suppose that obj is an S4 object with members member1 and member2. These members are accessible with the @ operator like obj@member1 and obj@member2. These elements can be returned to Java by constructing a new list like result\A1-list(m1=obj@member1, m2=obj@member2).

### 3 Handling Plots

Although the graphics drivers and the internals are implemented in C, most of the graphics functions and packages are written in the R language and this makes the R unique with its graphics library. RCaller handles a plot with the function startPlot() and receives a java.io.File reference to the generated plot. The function getPlot() returns an instance of the javax.swing.ImageIcon class which contains the generated image in a fully isolated way. A Java example is shown below:
RCaller caller = new RCaller();
RCode code = new RCode();
File plotFile = null;
ImageIcon plotImage = null;

caller.
setRscriptExecutable("/usr/bin/Rscript");

code.R_require("lattice");

try{
plotFile = code.startPlot();
xyplot(rnorm(100)~1:100, type=’l’)
");
}catch (IOException err){
System.out.println("Can not create plot");
}

caller.setRCode(code);
caller.runOnly();

plotImage = code.getPlot(plotFile);
code.showPlot(plotFile);

The method runOnly() is quite different from the method RunAndReturnResult(). Because the user only wants a plot to be generated, there is nothing returned by R in the example above. Note that more than one plots can be generated in a single run.
Handling R plots with a java.io.File reference is also convenient in web projects. Generated content can be easly sent to clients using output streams opened from the file reference. However, RCaller uses the temp directory and does not delete the generated files automatically. This may be a cause of a too many files OS level error which can not be caught by a Java program. However, cleaning the generated output using a scheduled task solves this problem.

### 4 Live Connection

Each time the method runAndReturnResult() is called, an Rscript instance is created to perform the operations. This is the main source of the inefficiency of RCaller. A better approach in the cases that R commands are repeatedly called is to use the method runAndReturnResultOnline(). This method creates an R instance and keeps it running in the background. This approach avoids the time required to create an external process, initialize the interpreter, and load packages in subsequent calls.
The example given below returns the determinants of a given matrix and its inverse in sequence, that is, it uses a single external instance to perform more than one operation.
double[][] matrix =
new double[][]{{5,4,5},{6,1,0},{9,-1,2}};
caller.setRExecutable("/usr/bin/R");
caller.setRCode(code);

code.clear();
caller.runAndReturnResultOnline("result");

System.out.println(
"Determinant is " +
caller.getParser().
getAsDoubleArray("d")[0]
);

caller.runAndReturnResultOnline("result");

System.out.println(
"Determinant of inverse is " +
caller.getParser().
getAsDoubleArray("t")[0]
);

This use of RCaller is fast and convenient for repeated commands. Since R is not thread-safe, its functions can not be called by more than one threads. Therefore, each single thread must create its own R process to perform calculations simultaneously in Java.

### 5 Monitoring the Output

RCaller receives the desired content as XML documents. The content is a list of the variables of interest which are manually created by the user or returned automatically by a function. Apart from the generated content, R produces some output to the standard output (stdout) and the standard error (stderr) devices. RCaller offers two options to handle these outputs. The first one is to save them in a text file. The other is to redirect all of the content to the standard output device. The example given below shows a conditional redirection of the outputs generated by R.
if(console){
caller.redirectROutputToConsole();
}else{
caller.redirectROutputToFile(
"output.txt" /* filename */,
true  /* append? */);
}

### 6 Conclusion

In addition to being a statistical software, R is an extendable library with its internal functions and external packages. Since the R interpreter was written mostly in C, linking to custom C/C++ programs is relatively simple. Unfortunately, calling R functions from Java is not straightforward. The prominent methods use JNI and TCP sockets to solve this problem. In addition, renjin offers a different perspective to this issue. It is a re-implementation of R in Java which is intended to be 100% compatible with the original. However, it is under development and needs help. Finally, RCaller is an alternative way of calling R from Java. It is packaged in a single jar and it does not require setup beyond the one-time installation of the R package Runiversal. It supports loading external packages, calling functions, handling plots and debugging the output generated by R. It is not the most efficient method compared to the alternatives, but users report that performance improvements in the latest revision and its simplicity of use make it an important tool in many applications.

### References

[Commons Math Developers(2010)]   Commons Math Developers. Apache Commons Math, Release 2.1. Available from http://commons.apache.org/math/download_math.cgi, Apr. 2010. URL http://commons.apache.org/math.
[Harner et al.(2009)Harner, Luo, and Tan]   E. Harner, D. Luo, and J. Tan. JavaStat: A Java/R-based statistical computing environment. Computational Statistics, 24(2):295–302, May 2009.
[R Development Core Team(2011)]   R Development Core Team. R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria, 2011. URL http://www.R-project.org/. ISBN 3-900051-07-0.
[RCaller Development Team(2011)]   RCaller Development Team. RCaller: A library for calling R from Java, 2011. URL http://code.google.com/p/rcaller.
[Satman(2010)]   M. H. Satman. Runiversal: A Package for converting R objects to Java variables and XML., 2010. URL http://CRAN.R-project.org/package=Runiversal. R package version 1.0.1.
[Urbanek(2009)]   S. Urbanek. How to talk to strangers: ways to leverage connectivity between R, Java and Objective C. Computational Statistics, 24(2):303–311, May 2009.
[Urbanek(2011a)]   S. Urbanek. rJava: Low-level R to Java interface, 2011a. URL http://CRAN.R-project.org/package=rJava. R package version 0.9-2.
[Urbanek(2011b)]   S. Urbanek. Rserve: Binary R server, 2011b. URL http://CRAN.R-project.org/package=Rserve. R package version 0.6-5.

A new research paper as a RCaller documentation is freely available at http://www.sciencedomain.org/abstract.php?iid=550&id=6&aid=4838#.U5YSoPmSy1Y