CARET Package in R - createDataPartition() function not found? - r

I am learning the CARET Package in R and I am trying to run to code in Max Kuhn's paper "A Short Introduction to the caret Package". When I run the following code, after successfully installing CARET:
data(Sonar)
set.seed(107)
# The outcome data are needed, the percentage of data in the training set & the format of the results
inTrain <- createDataPartition(y = Sonar$Class,p = .75, list = FALSE)
str(inTrain)
I get the following error that this function createDataPartition() is not found:
Error in createDataPartition(y = Sonar$Class, p = 0.75, list = FALSE) :
could not find function "createDataPartition"
> str(inTrain)
Error in str(inTrain) : object 'inTrain' not found
Any thoughts?
Best,
Mike

I had the same problem... The solution:
In your shell go to /var/folders/tn/f7md6x8j0b73tg9tp83hgm4r0000gn/T//RtmpChzV5D/downloaded_packages
cd /var/folders/tn/f7md6x8j0b73tg9tp83hgm4r0000gn/T//RtmpChzV5D/downloaded_packages
Look what you have there:
ls
If you have something like caret_6.0-80.tgz, you need to remove it:
sudo rm -r caret_6.0-80.tgz
I hope I've helped :)

Related

createDataPartition function not working in Rstudio

code:
validation_index <- createDataPartition(dataset$Accepectance.Rate, p=0.80, list=FALSE)
Error in createDataPartition(dataset$Accepectance.Rate, p = 0.8, list = FALSE) :
could not find function "createDataPartition"
My dataset is of 400 observations and 8 variables.
Could anyone suggest what's wrong with this, although I have install the required packages
You have to use library or require after installing if you want to use a function from the package caret. For example:
library(caret)
or
require(caret)
should be run before you use the function createDataPartition()
You can also use the function as caret::createDataPartition().

Error in ts(data) : R scoping issue

I am running my script
#!/usr/bin/env Rscript
require(gdata)
library('ggplot2')
library('forecast')
library('tseries')
df1 <- read.xls('us-gasprices.xls',header = TRUE)
new = df1$V2
data = tail(new,-1)
data = ts(data)
fit = auto.arima(data, seasonal=FALSE)
png(filename="residuals.png")
tsdisplay(residuals(fit), lag.max=40, main='(4,1,1) Model Residuals')
plot(m)
dev.off()
Output
Error in ts(data) : 'ts' object must have one or more observations
I have read this thread
I understand what Rob has explained that the problem is in scoping.
When I run my code from R terminal line by line then it works fine.
But when I run Rscript in Ubuntu terminal the above-mentioned problem occurs.
How to solve this?

Create a path diagram of a SEM model with a categorical response variable using semPaths()

I would like to create a path diagram of a SEM model with a categorical response variable using semPaths(). However I am running into an error:
library(lavaan)
library(semPlot)
table.7.5 <-read.table("http://www.da.ugent.be/datasets/Agresti2002.Table.7.5.dat",header=TRUE)
table.7.5$mental <- ordered(table.7.5$mental,levels = c("well","mild","moderate","impaired"))
model <- "mental ~ ses + life"
fit <- sem(model, data=table.7.5)
semPaths(fit,"std",edge.label.cex = 0.5, curvePivot=TRUE,layout = "tree")
Error is:
Error in colnames<-(*tmp*, value = "mental") :
attempt to set 'colnames' on an object with less than two dimensions
Thanks
The above solution solves the problem for the current CRAN version of semPlots(). In the meantime this issue has been solved in the development version. To install it run:
library(devtools)
install_github("SachaEpskamp/semPlot")
For more details read this:
https://github.com/SachaEpskamp/semPlot/issues/9
Someone helped me to solve the problem by replacing the content of cov by res.cov. I don't know why but when it's ordered data, lavaan puts the implied covariance matrix in res.cov instead of cov.
All you have to do is this:
fit#SampleStats#cov<-fit#SampleStats#res.cov
Before calling:
semPaths(fit,"std",edge.label.cex = 0.5, curvePivot=TRUE,layout = "tree")

R Leaps Package: Regsubsets - coef "Reordr" Fortran error

I'm using the R leaps package to obtain a fit to some data:
(My dataframe df contains a Y variable and 41 predictor variables)
require(leaps)
N=3
regsubsets(Y ~ ., data = df, nbest=1, nvmax=N+1,force.in="X", method = 'exhaustive')-> regfit
coef(regfit,id = N)
When I run the code more than once (the first time works fine) I get the following error when I run the coef command:
Error in .Fortran("REORDR", np = as.integer(object$np), nrbar = as.integer(object$nrbar), :
"reordr" not resolved from current namespace (leaps)
Any help with why this is happening would be much appreciated.
A.
I had to build the package from source inserting the (PACKAGE = 'leaps') argument into the REORDR function in the leaps.R file. It now works fine every time.
The solution is related to:
R: error message --- package error: "functionName" not resolved from current namespace

Error with gamsel R Package

I'm trying to use the gamsel R package to fit a sparse generalized additive model, and I can't seem to get it to work on real data. When I run on synthetic data as described in the package documentation, everything works well:
library(gamsel)
data=gendata(n=500,p=12,k.lin=3,k.nonlin=3,deg=8,sigma=0.5)
attach(data)
bases=pseudo.bases(X,degree=10,df=6)
gamsel.out=gamsel(X,y,bases=bases)
But when I run on real data, I get the following error:
library(gamsel)
X = as.matrix(read.csv("X.csv"),header=FALSE)
y = as.matrix(read.csv("y.csv"),header=FALSE)
gam_fit = gamsel(X,y)
Error in if (abs((df - current.df)/df) < 1e-04 | iterations == 1)
return(list(lambda = lambda, : missing value where TRUE/FALSE
needed
You can access sample data files that will reproduce this result here. Any thoughts about how to fix this error?

Resources