Paul.Rustomji at csiro.au
2010-Mar-22 23:54 UTC
[R] Problem with factors in lm() when using Rscript but not GUI
Hello R-help
I am having a curious problem. I am using a batch file to call an R script and
am getting an error when performing a lm() fit using factorial data only when
running in batch mode (see below). I do not get the error when executing the
same code in GUI mode in R 2.10.1. Nor do I get the error when using Rscript
version 2.9.2....
#######################################
#windows batch file contains this line to call tnp2.r r-script
#Rscript ./tnp2.r --slave
########################################
########################################
#tnp2.r - contents of tnp2.r script file are below
print(R.Version()$version.string)
#generate some data
x <- rep(c(1:12),3)
y <- 1:length(x)+rnorm(length(x))
print(x)
print(y)
#this bit works OK
xtemp <- lm(y ~ x )
print(xtemp)
print("got to here (without factor part)")
monthfac <- as.factor(x)
print(monthfac)
#this line below causes probs in R v2.10.1 when run from Rscript.....
xtemp2 <- lm(y ~ monthfac)
print(xtemp2)
stop("stopped on purpose",call.=F)
#end of file tnp2.r
###############################################
#error message is thus:
Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
could not find function "is"
Calls: lm ... model.matrix -> model.matrix.default -> contrasts<-
Execution halted
As I say - all works ok in GUI mode but not in Rscript mode (v2.10.1).....
This problem seemed to commence after upgrading to R 2.10.1 from R2.9.2, as the
same code works fine in Rscript in version 2.9.2.
Paul.
###########################> Sys.info()
sysname release
"Windows" "XP"
version nodename
"build 2600, Service Pack 3" "XXXX"
machine login
"x86" "xxxxxx"
user
"xxx"
$platform
[1] "i386-pc-mingw32"
$arch
[1] "i386"
$os
[1] "mingw32"
$system
[1] "i386, mingw32"
$status
[1] ""
$major
[1] "2"
$minor
[1] "10.1"
$year
[1] "2009"
$month
[1] "12"
$day
[1] "14"
$`svn rev`
[1] "50720"
$language
[1] "R"
$version.string
[1] "R version 2.10.1 (2009-12-14)"
Paul Rustomji
CSIRO Land and Water (Lucas Heights, NSW)
Paul Rustomji
2010-Mar-23 02:12 UTC
[R] Problem with factors in lm() when using Rscript but not GUI
Found the solution - already posted here http://n4.nabble.com/Rscript-question-td1067729.html#a1067729 Basically Rscript does not automatically load library(methods) like Rgui does... -- View this message in context: http://n4.nabble.com/Problem-with-factors-in-lm-when-using-Rscript-but-not-GUI-tp1678504p1678575.html Sent from the R help mailing list archive at Nabble.com.
Prof Brian Ripley
2010-Mar-23 05:57 UTC
[R] Problem with factors in lm() when using Rscript but not GUI
Soemone broke contrasts<- in 2.10.0 to require the 'methods' package, but this was corrected in R-patched on Jan 4. Thus you need to add 'methods' to the default packages in Rscript (see its help) or update your R. This has come up before, so a search of the archives was likely to have found it. On Tue, 23 Mar 2010, Paul.Rustomji at csiro.au wrote:> Hello R-help > > I am having a curious problem. I am using a batch file to call an R script and am getting an error when performing a lm() fit using factorial data only when running in batch mode (see below). I do not get the error when executing the same code in GUI mode in R 2.10.1. Nor do I get the error when using Rscript version 2.9.2.... > > > ####################################### > #windows batch file contains this line to call tnp2.r r-script > #Rscript ./tnp2.r --slave > ######################################## > > ######################################## > #tnp2.r - contents of tnp2.r script file are below > > print(R.Version()$version.string) > > #generate some data > x <- rep(c(1:12),3) > y <- 1:length(x)+rnorm(length(x)) > > print(x) > print(y) > > #this bit works OK > xtemp <- lm(y ~ x ) > print(xtemp) > print("got to here (without factor part)") > > monthfac <- as.factor(x) > print(monthfac) > > #this line below causes probs in R v2.10.1 when run from Rscript..... > xtemp2 <- lm(y ~ monthfac) > > print(xtemp2) > stop("stopped on purpose",call.=F) > > #end of file tnp2.r > ############################################### > > > > #error message is thus: > > Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") : > could not find function "is" > Calls: lm ... model.matrix -> model.matrix.default -> contrasts<- > Execution halted > > > As I say - all works ok in GUI mode but not in Rscript mode (v2.10.1)..... > > This problem seemed to commence after upgrading to R 2.10.1 from R2.9.2, as the same code works fine in Rscript in version 2.9.2. > > Paul. > > ########################### >> Sys.info() > sysname release > "Windows" "XP" > version nodename > "build 2600, Service Pack 3" "XXXX" > machine login > "x86" "xxxxxx" > user > "xxx" > > $platform > [1] "i386-pc-mingw32" > > $arch > [1] "i386" > > $os > [1] "mingw32" > > $system > [1] "i386, mingw32" > > $status > [1] "" > > $major > [1] "2" > > $minor > [1] "10.1" > > $year > [1] "2009" > > $month > [1] "12" > > $day > [1] "14" > > $`svn rev` > [1] "50720" > > $language > [1] "R" > > $version.string > [1] "R version 2.10.1 (2009-12-14)" > > > > > > > Paul Rustomji > CSIRO Land and Water (Lucas Heights, NSW) > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595