Here is a ficticious dataset consisting of 10 genetic lines and a set of 7 parameters:
data.1<-read.csv(text="
line,a,b,c,d,TF1,TF2,TF3
line1,0.716954406,0.948933475,0.723180846,0.122529549,-1,-1,-1
line2,0.443478142,0.052760906,0.814897888,0.072935389,1,-1,1
line3,0.362944986,0.892056197,0.243860823,0.197642553,1,1,1
line4,0.516451924,0.742629204,0.170810671,0.886592564,-1,-1,-1
line5,0.262766818,0.676690503,0.585481859,0.544782573,1,1,1
line6,0.437307171,0.326012372,0.194698638,0.992025701,1,1,1
line7,0.018027541,0.241761164,0.068979207,0.170437435,1,-1,1
line8,0.663364588,0.237946201,0.056954659,0.953926657,1,1,1
line9,0.062131129,0.066129381,0.156008808,0.235503941,-1,-1,-1
line10,0.018027541,0.241761164,0.06897920,0.170437435,-1,-1,-1
")
What I am trying to do is to apply the folowing function to each line and get a sim value:
flow.function <- function(a, b, c, d, TF1, TF2, TF3) {
sim <- 44.18 + 4.026*(a-12.7) + 0.195*(b-18.21) - 1.363*(c-27.4) - 0.60*(d-16.12) - 1.3*TF1 - 2.279*TF2 + 1.59*TF3
return(sim)
}
Then I apply the function row wise:
data.1$sim.results <- apply(data.1, 1, flow.function)
And this is the error I am getting:
Error in a - 12.7 : non-numeric argument to binary operator
I undertand the error is pointing out to the "12.7" value in the formula, what I have no idea why I get this error since 12.7 is a number. I am surely missing something basic.
Any help will be really appreciated.
Because you did not provide additional function arguments in apply (it only expects one function argument). Try the following:
data.1<-read.csv(text="
line,a,b,c,d,TF1,TF2,TF3
line1,0.716954406,0.948933475,0.723180846,0.122529549,-1,-1,-1
line2,0.443478142,0.052760906,0.814897888,0.072935389,1,-1,1
line3,0.362944986,0.892056197,0.243860823,0.197642553,1,1,1
line4,0.516451924,0.742629204,0.170810671,0.886592564,-1,-1,-1
line5,0.262766818,0.676690503,0.585481859,0.544782573,1,1,1
line6,0.437307171,0.326012372,0.194698638,0.992025701,1,1,1
line7,0.018027541,0.241761164,0.068979207,0.170437435,1,-1,1
line8,0.663364588,0.237946201,0.056954659,0.953926657,1,1,1
line9,0.062131129,0.066129381,0.156008808,0.235503941,-1,-1,-1
line10,0.018027541,0.241761164,0.06897920,0.170437435,-1,-1,-1
")
flow.function <- function(a, b, c, d, TF1, TF2, TF3) {
sim <- 44.18 + 4.026*(a-12.7) + 0.195*(b-18.21) - 1.363*(c-27.4) - 0.60*(d-16.12) - 1.3*TF1 - 2.279*TF2 + 1.59*TF3
return(sim)
}
data.1$sim.results <- mapply(flow.function, data.1$a, data.1$b, data.1$c, data.1$d, data.1$TF1, data.1$TF2, data.1$TF3)
Here is my integrand()
integrand<-function(x,vecC)
{
as.numeric((2/(b-a))*vecC%*%as.matrix(cos((x-hat.a)
*(seq(0,N-1,length=N)*pi/(b-a)))))
}
it can produce the value. For example, for
a<-1
b<-10
vecC<-t(as.matrix(rnorm(80)))
hat.a<--1.2
N<-80
I get
> integrand(1.4,vecC)
[1] -0.3635195
but I met problem when I run the following code for integration
> integrate(function(x){integrand(x,vecC)},upper = 3.4,lower = 1)$value
and the error message is
Error in integrate(function(x) { :
evaluation of function gave a result of wrong length
In addition: Warning message:
In (x - hat.a) * (seq(0, N - 1, length = N) * pi/(b - a)) :
longer object length is not a multiple of shorter object length
If you read the help page for integrate you will see that the function passed to integrate should return a vector.
So the solution to your error is to use Vectorize like this
Define your function separately as
f <- function(x){integrand(x,vecC)}
Now define a vectorized version of this function like so
fv <- Vectorize(f,"x")
and then
integrate(fv,upper = 3.4,lower = 1)$value
will give you a result.
My formula is
$$\int_{a}^{b}\big(exp(x) +1 \big)^{i\cdot u} \cdot \text{cos} \big((x-\alpha) u \big)dx$$
Here is my R code for integration:
a<--2; b<-2; u<-0.15; alpha<-0.8
integrand<-function(x)
{
(exp(x)+1)^(1i*u)*cos((x-alpha)*u)
}
integrate(integrand,lower=a,upper = b)
After running this code, I got the error message from R:
Error in integrate(integrand, lower = a, upper = b) :
evaluation of function gave a result of wrong type
Where is my mistake? Thanks!
Apparently, integrate cannot do complex integration. Use elliptic package's myintegrate instead:
a<-2
b<-2
u<-0.15
alpha<-0.8
integrand<-function(x) {
(exp(x)+1)^(1i * u)*cos((x-alpha)*u)
}
myintegrate(integrand,lower=a,upper = b)
Using pvclust::pvclust , I got an error
Error in solve.default(crossprod(X, X/vv)) : Lapack routine dgesv:
system is exactly singular: U[2,2] = 0 Calls: ...
pvclust.merge -> lapply -> FUN -> msfit -> solve -> solve.default
Execution halted
I don't want stop analysis even if crossprod(X, X/vv) be singular matrix, so I tried to insert an if {...} block in pvclust::msfit to check whether crossprod(X, X/vv) is singular or not by matrixcalc::is.singular.matrix, and if it is so, return NA and continue.
After saved my.msfit.R which contain msfit which incerted if(!is.singular.matrix(...)) {...}else{...} in original pvclust::msfit,
methods::insertSource('/myFuncDir/my.msfit.R',package="pvclust",functions='msfit')
But I got error below
Error in assign(this, thisObj, envir = envwhere) :
cannot change value of locked binding for 'msfit'
In addition: Warning message:
In methods::insertSource(filename, package = "pvclust", functions = "msfit", :
cannot insert these (not found in source): "msfit"
Is there any solution? Should I request of the author of the pvclust package?
== Below added after posting ==
An accurate advice was given in comments to use try/catch syntax, but I don't think it will give me solution.
Concerning my poor English skill, I present a toy sample which tells the situation.
fun.a <- function(a1,a2,a3,a4){
sum1 <- a1 + a2
sum2 <- a2 + a3
sum3 <- a3 + a4
return(list(sum1,sum2,sum3))
}
fun.a(1,2,3,'Char')
Because that the sum3 will be an error, so fun.a(1,2,3,'Char') returns error.
But, I want to return
List [sum1, sum2, NaN]
If I use tryCatch(...,error=expr), sum1 to sum3(actually, solve(...)in pvclust::msfit) should be wrapped.
But, fun.a(msfit) is inner function of locked package(pvclust).
Lately I've worked with kernlab library in R-statistical software. In specific, I use the ksvm function to identify patterns. I'm trying to implement a new kernel in SVM, my code is:
<gnndot<-function (sigma = 1, degree=2)
{
rval <- function(x, y = NULL) {
return(exp(-sigma * ( sqrt(-(round(2 * crossprod(x, y) - crossprod(x) - crossprod(y), 9)))^degree ) ))
}
}
library(mvtnorm)
library(kernlab)
x <- rmvnorm(n=500, mean=c(1,1.5,2,2.5,3), sigma=diag(5))
class<-rep(c("ONE","TWO","THREE","FOUR","FIVE"),each=100)
ksvm(x,class,type="C-svc",kernel ="gnndot",kpar=list(sigma=0.05,degree=1.5),C=600,cross=5)>
The result that I got is: Error in match.arg(kernel, c("rbfdot", "polydot", "tanhdot", "vanilladot", : 'arg' should be one of “rbfdot”, “polydot”, “tanhdot”, “vanilladot”, “laplacedot”, “besseldot”, “anovadot”, “splinedot”, “matrix”
This is, I must define this "user-defined" kernel as one of the already designed kernels. Is it possible to use the kernel here defined (gnndot) as one usable by the function ksvm?