`cv.glmnet` works in RStudio but not RScript
RScript
has this "lovely" feature of not loading the (base package) methods for you.
So all you need here is an additional
require(methods)
or
suppressMessages(library(methods))
For what it is worth, the littler command-line and scripting front-end to R that Jeff Horner and I wrote defaults to loading methods for you...
Error with Rscript call, but not R IDE
It turns out that one of my Rscript calls had a space in it (e.g.Rscript my thing
), such that there was no data in the dataframe and thus, the as.Date()
error.
I was able to simulate the error as follows (props to Dirk for the original):
Rscript -e 'print(as.Date(0, format="%Y-%m-%d"))'
Error in as.Date.numeric(0, format = "%Y-%m-%d") :
'origin' must be supplied
Calls: print -> as.Date -> as.Date.numeric
Execution halted
For the system call, I added shQuote() within the Rscript call, which solved the problem.
my_thing = 'something with spaces'
system(paste("Rscript my_file.R, shQuote(my_thing)"))
TLDR: The error was in the Rscript call, Dirk's advice to break the problem down into it's most simple form was excellent advice.
R glmnet as.matrix() error message
For some reason glmnet prefers data.matrix()
to as.matrix()
cv1 <- cv.glmnet(data.matrix(t2[,-c(1,2,7,12)]), t2[,2], family="multinomial")
should do the job.
Paste0 working in Rstudio, not in Rscript
In this line
sqljoin <- paste("select * from DBO.", tablevar,
" p join DBO.TABLE2 l on (p.ID1=l.ID2) where p.ID1 =", "'", row, "'",
sep = " ", collapse = ",")
you use row
before defining it. row
is a function, hence the error message.
Getting errors installing glmnet package using R studio
The problem is here
gfortran -fpic -g -O2 -fdebug-prefix-map=/build/r-base-oNcpyf/r-base-3.5.1=. -fstack-protector-strong -c glmnet5dp.f -o glmnet5dp.o
gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -fpic -g -O2 -fdebug-prefix-map=/build/r-base-oNcpyf/r-base-3.5.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c glmnet_init.c -o glmnet_init.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o glmnet.so glmnet5dp.o glmnet_init.o -lgfortran -lm -lquadmath -L/usr/lib/R/lib -lR
/usr/bin/x86_64-linux-gnu-ld: cannot find -lgfortran
make: gfortran: cannot find -lgfortran
You either don't have a fortran compiler or its not accessible by your R instance. How you fix that depends on your operating system,
Solution:-
Type these two lines in your terminal, direct quote:
curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
sudo tar fvxj gfortran-4.8.2-darwin13.tar.bz2 -C /
It will download you the gfortran for Mavericks (which is missing in your system at the moment) and will install it in your system. At least, this will solved your problem.
Difference between glmnet() and cv.glmnet() in R?
glmnet() is a R package which can be used to fit Regression models,lasso model and others. Alpha argument determines what type of model is fit. When alpha=0, Ridge Model is fit and if alpha=1, a lasso model is fit.
cv.glmnet() performs cross-validation, by default 10-fold which can be adjusted using nfolds. A 10-fold CV will randomly divide your observations into 10 non-overlapping groups/folds of approx equal size. The first fold will be used for validation set and the model is fit on 9 folds. Bias Variance advantages is usually the motivation behind using such model validation methods. In the case of lasso and ridge models, CV helps choose the value of the tuning parameter lambda.
In your example, you can do plot(reg) OR reg$lambda.min to see the value of lambda which results in the smallest CV error. You can then derive the Test MSE for that value of lambda. By default, glmnet() will perform Ridge or Lasso regression for an automatically selected range of lambda which may not give the lowest test MSE. Hope this helps!
Hope this helps!
Related Topics
Ggplot Aes_String Does Not Work Inside a Function
Multiply Permutations of Two Vectors in R
How to Convert Ensembl Id to Gene Symbol in R
Ggplot2: Overlay Density Plots R
How to Subset from a List in R
Ggplot2: Define Plot Layout with Grid.Arrange() as Argument of Do.Call()
How to Increase Size of the Points in Ggplot2, Similar to Cex in Base Plots
R Glmnet:"(List) Object Cannot Be Coerced to Type 'Double' "
How Does Gganimate Order an Ordered Bar Time-Series
How to Manually Set Colors in a Bar Chart
Unique Elements of Two Vectors
Predicting Lda Topics for New Data
Remove Strip Background Keep Panel Border
How to Jitter Text to Avoid Overlap in a Ggplot2 Scatterplot