How to use R in Weka
Remarks#
Why use R in Weka?
- R is a powerful tool for preprocessing data
- R has a huge number of libraries and keeps growing
- R in Weka, can easily get data from, process it, and pass to Weka seamlessly
How to setup R in Weka
For Mac User
-
replace the old info.Plist with the new one given by Mark Hall
-
download and install R
-
install
rJava
inside R withinstall.packages(‘rJava’)
-
install
Rplugin
withWeka Package Manager
-
go to
weka 3-8-0
folder (if it is the version you are using), and open its terminal, and -
run the following 2 lines of codes (thanks to Michael Hall)
export R_HOME=/Library/Frameworks/R.framework/Resources
java -Xss10M -Xmx4096M -cp .:weka.jar weka.gui.GUIChooser -
to make life easier, inside a directory where you want to work with weka, save the code above into a file named as
weka_r.sh
-
make it executable, inside this directory’s terminal, run the code below:
chmod a+x weka_r.sh
-
paste
weka.jar
from weka 3-8-0 into the directory and run the code below:./weka_r.sh
Now, you are ready to go. Next time, you just need to go to the directory’s terminal and run ./weka_r.sh
to start R with Weka.
How to receive data from Weka?
open Weka from terminal:
go to directory of Weka 3-8-0
, open its terminal, run the following code:
java -jar weka.jar
data through Weka Explorer:
preprocess
panel, clickopen file
, choose a data file fromweka data folder
;- go to
R console
panel, type R scripts insideR console box
.
data through Weka KnowledgeFlow:
Data mining processes
panel, clickDataSources
to chooseArffLoader
for example, click it onto canvas;- double-click
ArffLoader
to load a data file Scripting
panel, clickRscriptExecutor
onto canvasoption
+ clickArffLoader
, selectdataset
, then clickRScript Executor
to link them- double click
RScript Executor
to type R script, or - click
Settings
and selectR Scripting
to use R console with weka’s data
Playing R Codes
-
load
iris.arff
with either Explorer or KnowledgeFlow; -
try
Plotting inside R Console
example above
Plotting inside R Console
The following Codes can be found from Weka course
Given iris.arff
is loaded in weka, inside Weka Explorer’s R console
or Weka KnowledgeFlow’s R Scripting
, you can play with the following codes to make beautiful plots:
library(ggplot2)
ggplot(rdata, aes(x = petallength)) + geom_density()
ggplot(rdata, aes(x = petallength)) + geom_density() + xlim(0,8)
ggplot(rdata, aes(x = petallength)) + geom_density(adjust = 0.5) + xlim(0,8)
ggplot(rdata, aes(x = petallength, color = class)) + geom_density(adjust = 0.5) + xlim(0,8)
ggplot(rdata, aes(x = petallength, color = class, fill = class)) + geom_density(adjust = 0.5) + xlim(0,8)
ggplot(rdata, aes(x = petallength, color = class, fill = class)) + geom_density(adjust = 0.5, alpha = 0.5) + xlim(0,8)
library(reshape2)
ndata = melt(rdata)
ndata
ggplot(ndata, aes(x = value, color = class, fill = class)) + geom_density(adjust = 0.5, alpha = 0.5) + xlim(0,8) + facet_grid(variable ~ .)
ggplot(ndata, aes(x = value, color = class, fill = class)) + geom_density(adjust = 0.5, alpha = 0.5) + xlim(0,8) + facet_grid(. ~ variable)
ggplot(ndata, aes(y = value, x = class, colour = class)) + geom_boxplot() + facet_grid(. ~ variable)