I wrapped up the caret functionality to use with different random splits (variable i) and currently stacked over the problem - I don't know how to loop over the methods. paste doesn't work for me.
methods <- c("svmLinear","svmRadial")
for (M in methods) {
for (i in c(1:5)) {
data_load(act_file = "act.txt",
inact_file = "inact.txt")
sets(Rand=i)
mod_parms(k_folds=5)
modeling_y_testing(method = M, metric='ROC',tuneLength=10)
rm(list = ls())
}
}
the error is following
object 'M' not found
In addition: Warning message:
In .local(x, ...) : Variable(s) `' constant. Cannot scale data.
Execution halted
I suppose I need to convert the variable in methods into some sort of a character that should be accepted to modeling_y_testing (a caret-based function), but I don't know. You help is very appreciated.
Your problem is
`rm(list = ls())`
This function removes the variable M. So on the second iteration of the loop
`for(i in 1:5){`
we get the error
object `M` not found
Related
I can't figure out a simple problem of how to call a variable name from a dataframe passed as string( function input).
I have a function defined as:
box_lambda = function(Valuename,data1){
data1[,Valuename]=ifelse(data1[,Valuename]==0,0.000001,data1[,Valuename])
b= boxcox(get(Valuename) ~ Age.Group+Sex , data = data1)
lambda <- b$x[which.max(b$y)]
return(lambda)
}
But this doesn't work as I get error:
Error in eval(f): 'list' object cannot be coerced to type 'double'
I tried
data1[[Valuename]]=ifelse(data1[Valuename]]==0,0.000001,data1[[Valuename]])
Any help is appreciated!
First you lost a bracket necessary to address a field:
data1[[Valuename]]
You can also use a seties of other approaches from [here][1] and from [here][2]. For instance you can use:
library(dplyr)
data %>%
filter(!!as.name(Valuename) == 0)
So finally you can use :
data1[[Valuename]][data1[[Valuename]]==0] <-0.000001
This script will replace 0 with epsilon and leave the other values.
[1]: https://stackoverflow.com/a/74173690/5043424
[2]: https://stackoverflow.com/a/48219802/5043424
Having trouble setting up these integral terms in R. I created "term1" to more easily insert it into the integral code, but I keep getting various error messages after trying different codes. any help or spotting the issue would be appreciated.
##
S<-readline(prompt="Enter underlying instrument price:")
X<-readline(prompt="Enter strike price:")
V<-readline(prompt="Enter absolute volatility in dollars:")
r<-readline(prompt="Enter risk-free rate (%):")
q<-readline(prompt="Enter dividend yield (%):")
T<-readline(prompt="Enter time to maturity, in fraction of years:")
t=0
##
S<-as.numeric(S)
X<-as.numeric(X)
V<-as.numeric(V)
r<-as.numeric(r)/100
q<-as.numeric(q)/100
T<-as.numeric(T)
##Bond Price
B<-exp(r*(T-t))
##Volatility
vol<-function(start,end,rate,yield,B) {
if(r==q){
V*(sqrt((B-1)/(2*(r-q))))
}
else{
V*(sqrt(T-t))
}
}
##d
d<-(S*B-X)/vol()
##N(d)
term1<-(exp(-(r^2)/2)/sqrt(2*pi))
#Call
Nc<-function(term1){
Nc<-((integrate(term1,-Inf,d)))}
#Put
Np<-function(term1){
Np<-(-(integrate(term1,-Inf,d)))}
These are the errors i am getting
> Nc(term1)
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'term1' of mode 'function' was not found
> Nc()
Error in match.fun(f) : argument "term1" is missing, with no default
Well, the big question is: Have you read the documentation for function integrate ?
The first argument you assign to that function has to be a function.
The second problem is, that function Nc, which you have defined, does not return any output (you can fix that by not assigning the result of integrate to an object and instead just returning it. Same goes for the Np. One more problem with Np is, that it actually returns an error "unexpected argument to unary operator"
Anyway, here is the code with some changes:
## Assign some "random" numbers to the variables, so that I can run the script
S<-100
X<-110
V<-3
r<-10
q<-15
# Is it a good idea to have variable called T? T stands also for TRUE
T <- 10
t=0
##
S<-as.numeric(S)
X<-as.numeric(X)
V<-as.numeric(V)
r<-as.numeric(r)/100
q<-as.numeric(q)/100
T<-as.numeric(T)
##Bond Price
B<-exp(r*(T-t))
##Volatility
vol<-function(start,end,rate,yield,B) {
if(r==q){
V*(sqrt((B-1)/(2*(r-q))))
}
else{
V*(sqrt(T-t))
}
}
##d
d<-(S*B-X)/vol()
# make term1 a function of r, so that there is actually something to integrate...
term1<-function(r) {(exp(-(r^2)/2)/sqrt(2*pi))}
# Do not assign the result of integration to an object, otherwise there will be no output from the function
Nc<-function(term1){
((integrate(term1,-Inf,d)))}
# In order to use the minus sign, you have to extract the value from the result of function "integrate"
Np<-function(term1){
(-(integrate(term1,-Inf,d)$value))}
Normally I wonder where mysterious errors come from but now my question is where a mysterious lack of error comes from.
Let
numbers <- c(1, 2, 3)
frame <- as.data.frame(numbers)
If I type
subset(numbers, )
(so I want to take some subset but forget to specify the subset-argument of the subset function) then R reminds me (as it should):
Error in subset.default(numbers, ) :
argument "subset" is missing, with no default
However when I type
subset(frame,)
(so the same thing with a data.frame instead of a vector), it doesn't give an error but instead just returns the (full) dataframe.
What is going on here? Why don't I get my well deserved error message?
tl;dr: The subset function calls different functions (has different methods) depending on the type of object it is fed. In the example above, subset(numbers, ) uses subset.default while subset(frame, ) uses subset.data.frame.
R has a couple of object-oriented systems built-in. The simplest and most common is called S3. This OO programming style implements what Wickham calls a "generic-function OO." Under this style of OO, an object called a generic function looks at the class of an object and then applies the proper method to the object. If no direct method exists, then there is always a default method available.
To get a better idea of how S3 works and the other OO systems work, you might check out the relevant portion of the Advanced R site. The procedure of finding the proper method for an object is referred to as method dispatch. You can read more about this in the help file ?UseMethod.
As noted in the Details section of ?subset, the subset function "is a generic function." This means that subset examines the class of the object in the first argument and then uses method dispatch to apply the appropriate method to the object.
The methods of a generic function are encoded as
< generic function name >.< class name >
and can be found using methods(<generic function name>). For subset, we get
methods(subset)
[1] subset.data.frame subset.default subset.matrix
see '?methods' for accessing help and source code
which indicates that if the object has a data.frame class, then subset calls the subset.data.frame the method (function). It is defined as below:
subset.data.frame
function (x, subset, select, drop = FALSE, ...)
{
r <- if (missing(subset))
rep_len(TRUE, nrow(x))
else {
e <- substitute(subset)
r <- eval(e, x, parent.frame())
if (!is.logical(r))
stop("'subset' must be logical")
r & !is.na(r)
}
vars <- if (missing(select))
TRUE
else {
nl <- as.list(seq_along(x))
names(nl) <- names(x)
eval(substitute(select), nl, parent.frame())
}
x[r, vars, drop = drop]
}
Note that if the subset argument is missing, the first lines
r <- if (missing(subset))
rep_len(TRUE, nrow(x))
produce a vector of TRUES of the same length as the data.frame, and the last line
x[r, vars, drop = drop]
feeds this vector into the row argument which means that if you did not include a subset argument, then the subset function will return all of the rows of the data.frame.
As we can see from the output of the methods call, subset does not have methods for atomic vectors. This means, as your error
Error in subset.default(numbers, )
that when you apply subset to a vector, R calls the subset.default method which is defined as
subset.default
function (x, subset, ...)
{
if (!is.logical(subset))
stop("'subset' must be logical")
x[subset & !is.na(subset)]
}
The subset.default function throws an error with stop when the subset argument is missing.
I copied the function from the web:
# function used to predict Best Subset Selection Regression
predict.regsubsets = function(object, newdata, id, ...) {
form = as.formula(object$call[[2]])
mat = model.matrix(form, newdata)
coefi = coef(object, id = id)
mat[, names(coefi)] %*% coefi
}
However, when I try to use the above function within another function , I kept getting the following error.
library(leaps)
abc <- function(){
regfit <- regsubsets(lpsa ~.,data = XTraining, nvmax = 8)
predict.regsubsets(regfit, data = XTesting, id = 1)
}
abc()
Error in object$call[[2]] : subscript out of bounds
I read ?call in R already. But it doesn't help me understanding what went wrong here, in particular what is $call[[2]] ?
How can I edit the function above such that when I call the above function inside another function I won't get an error ?
The culprit is the line
form = as.formula(object$call[[2]])
This implies that object (which is the variable you pass to the function, in your example regfit) has a member called call, which is a list with at least two elements. [[ ]] is the R operator used to take the elements of a list.
For instance:
> a <- list(1:10, 1:5, letters[15:20])
> a[[2]]
[1] 1 2 3 4 5
> a[[3]]
[1] "o" "p" "q" "r" "s" "t"
However
> a[[5]] # This does not work, as a only has three elements
Error in a[[5]] : subscript out of bounds
You should not check ?call but rather the help for the function that generates object, in your case regsubsets.
As you can see from ?regsubsets, or by using str(regfit), that function does not return an object with a member named call.
To get the formula from a regsubsets object you need to look at the obj member of the summary.
For instance you could use:
sm <- summary(regfit)
sm$obj$call
Your mistake is in the function abc. The argument in the predict.regsubsets is called newdata, but you refer to is as data....
The object is probably the output from a previous analysis (look at the place you got the code from what function). The line form = as.formula(objects$call[[2]]) extracts the formula used to create the object and stores it in form. In the next lines it is used to create the model matrix of the new data, and finally uses it to predict the new data.
I'm new to creating classes and methods in R and am running into a problem that I haven't found much documentation on. I have created a class, 'DataImport', and am trying to add the method below:
DataImport$methods(reducedImport <- function(filePathOne, dataFrame)
{
}
)
When I run this code I'm getting the following error:
Error in DataImport$methods(reducedImport <- function(filePathOne, :
Arguments to methods() must be named, or one named list
I was able to add a method directly before this one and it worked fine but this one doesn't. I don't quite understand why that would be the case or how to fix it.
As Dason mentioned in the comment, your problem is with assignment. Let's create a simple example:
c1 = setRefClass("c1", fields = list( data = "numeric"))
c1$methods(m1 = function(a) a)
Now a quick test:
x = c1$new(data=10)
x$m1(1)
However,
R> c1$methods(m2 <- function(a) a)
Error in c1$methods(m2 <- function(a) a) :
Arguments to methods() must be named, or one named list
gives the error you see. The reason for this is that the <- operator is slightly different from the = operator. This in general doesn't matter (but it does here).