[R] Regarding the 'R' Load Command

Godavarthi, Murali MGodavarthi at dpscs.state.md.us
Wed May 19 17:40:00 CEST 2010

Hi Steve,

Thanks so much for your inputs! I was actually trying to implement your
suggestions, I get the below error (please see the results of predict
command below). 

What we are trying to do is to feed in values for about 23
characteristics of an individual, and use the randomForest() function to
determine if the individual is a violent offender. Expected output is 0
or 1, indicating yes/no. 

Am I going wrong again? Here is what I was doing:

1) Created a text file with following data:
"imurder" "itheft" "irobbery" "iassault" "idrug" "iburglary" "igun"
"psych" "Freq" "priors" "firstage" "intage" "sex" "race" "marstat"
"empac" "educ" "zipcode" "suspendmn" "drugs" "alco" "probation" "parole"
"10" 0 0 0 1 0 0 0 0 0 58 19 19 "1" "BLACK" "SINGLE" "UNEMPLD" 0 21215 0
0 0 1 0

The above format in which the text file was created is in the same
format as the one which is already working, but has characteristics of
about 290 individuals fed-in instead of just one individual as above.
Not sure why this doesn't work!

2) Executed the below command sequence:

> library(randomForest)
randomForest 4.5-34
Type rfNews() to see new features/changes/bug fixes.

> load("C://Program Files//R//R-2.10.1//bin//rfoutput")

> testmurali<-read.table("ex.data",T)

> load(testmurali)
Error in load(testmurali) : bad 'file' argument

> load("testmurali")

> names(testmurali)
 [1] "imurder"   "itheft"    "irobbery"  "iassault"  "idrug"
"iburglary" "igun"      "psych"     "Freq"      "priors"   
[11] "firstage"  "intage"    "sex"       "race"      "marstat"   "empac"
"educ"      "zipcode"   "suspendmn" "drugs"    
[21] "alco"      "probation" "parole"   

> predict(rfoutput,newdata=testmurali,type="response")
Error in predict.randomForest(rfoutput, newdata = testmurali, type =
"response") : 
  Type of predictors in new data do not match that of the training data.

The model rfoutput used in the above predict command is also based on a
working example with similar data. 

Also, does load command accept a data string input directly (without
storing it into a file and then providing path of the file as a string)?

Please suggest. Thanks in advance!

Best Regards,
Murali Godavarthi
410-585-3746 (w)

ITCD - DPSCS Data Mining

-----Original Message-----
From: Steve Lianoglou [mailto:mailinglist.honeypot at gmail.com] 
Sent: Tuesday, May 18, 2010 4:13 PM
To: Godavarthi, Murali
Cc: r-help at r-project.org
Subject: Re: [R] Regarding the 'R' Load Command


On Tue, May 18, 2010 at 2:49 PM, Godavarthi, Murali
<MGodavarthi at dpscs.state.md.us> wrote:
> Hi,
> I'm new to 'R' and need some help on the "Load" command. Any responses
> will be highly appreciated. Thanks in advance!
> As per manuals, the "Load" command expects a binary file input that is
> saved using a "save" command.

Or a path to the file ...

> However it is required that we need to
> call the 'R' program from
> Java web application using RJava, and pass a string to the 'R" program
> instead of a binary file. Is it possible?

Yes, pay closer attention to the description for the "file" argument
in the load function (see ?load):

"""a (readable binary) connection **or a character string** giving the
name of the file to load"""

(emphasis mine)

> I was exploring the options of using TextConnections, file connections
> and other types of connections in order to read a stream of input
> (either from a file, stdin etc). I am able to read the string, but the
> Save and Load commands are not accepting the string input. Here is the
> sequence of commands I tried running, and the error received. There is
> no clue on this error, especially when trying to use the eval function
> in randomForest package, even on the internet. Can anyone help please!
>> library(randomForest)
> randomForest 4.5-34
> Type rfNews() to see new features/changes/bug fixes.
>> load("C://Program Files//R//R-2.10.1//bin//rfoutput")
>> zz <- file("ex.data", "w")
>> cat("\"imurder\" \"itheft\" \"irobbery\" \"iassault\" \"idrug\"
> \"iburglary\" \"igun\" \"psych\" \"Freq\" \"priors\" \"firstage\"
> \"intage\" \"sex\" \"race\" \"marstat\" \"empac\"
> \"educ\" \"zipcode\" \"suspendmn\" \"drugs\" \"alco\" \"probation\"
> \"parole\"",file = zz, sep = "\n", fill = TRUE)
>> cat("\"10\" 0 0 0 1 0 0 0 0 0 58 19 19 \"1\" \"BLACK\" \"SINGLE\"
> \"UNEMPLD\" 0 21215 0 0 0 1 0",file = zz, sep = "\n", fill = TRUE)

What are you trying to do here?
It looks like you want to save a table of sorts. First create your
data into a data.frame, then save that data.frame to a file using
write.table (or write.csv, etc).

>> save(zz, file = "testmurali", version = 2)

You're saving a file "object" here, not the contents of the file.
Once you successfully serialize your data into a text file, just load
it from "like normal" using read.table (or similar).

Anyway, I'm not sure what we're talking about here, but in short:

1. You need to make sure that you are correctly saving what you think
you're saving.
2. You can pass a character string to the `load` function, so you can
send it through (over from) java as  you wich.
3. I don't think you really want to deal with load/save here, because
it looks like you are dealing with some tab delimited file -- in which
case use read.table (or similar) and load it that way. You can, of
course, still use save/load, but make sure you save/load the right
thing (not a file object like you're doing here).

Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

More information about the R-help mailing list