I am new to R code and trying different functions in R. However when I tried to use the sqrt function, it always give me a error: argument "which" is missing. When I tried to see what sqrt really is, this was showing in my console. y = if(which==1){sqrt(x)} else {log(x)}. Does anyone have idea why my sqrt function is different than the other's?
Related
I think this should be a simple problem, but I am struggling to find a solution elsewhere. I have a function with several inputs. I would like to be able to find the value of a single input that results in the function returning 0 given constants for the other inputs.
simplified version:
funct = function(x,y) {x+y
}
Given a value of 10 for x, I would like R to solve for what y would need to be to return the value of 0 for the function. Is there a simple way to solve these optimization/solver challenges?
With Maxima, I want to plot the value of a parameter depending on time by solving an equation for that specific parameter. I am new to Maxima and I already struggle with the beginning of my calculations.
I use the following equation m which I want to solve for L:
m= m_I - (m_I-m_R)/(1+%e^(-s_R*(t-L)))
solve(%,L);
which gives me
L=(t*s_R-log(m_I/(m-m_R)-m/(m-m_R)))/s_R
as output. If I now assign values to all parameters except L and t
ev(%,m=0.5,m_I=1,m_R=0.1,s_R=0.01);
plot2d(%,[t,0,10]);
I get the error message
"plot2d: expression evaluates to non-numeric value everywhere in
plotting range. plot2d: nothing to plot."
I know this is very basic but I still don't know what I am doing wrong. I also tried to use a function m(t):=... instead of an expression m=..., with the same result.
Note that solve has returned a list containing one element, which is an equation. In order to plot the result, you need to isolate the right-hand side of the equation, because that's what plot2d understands (it doesn't know what to do with the output of solve otherwise).
My advice is to get the part of the solve result that you want first, and then plot that. Something like:
solve (...);
my_equation : %[1];
my_equation_rhs : rhs(%);
plot2d (my_equation_rhs, [t, 0, 10]);
It is a deficiency of plot2d that it doesn't know what to do with the result of solve; sorry about that.
Having come from Matlab I am struggling to work out why the following does not work:
plot(x=rand(10),y=rand(10))
Produces a graph correctly.
x=rand(10)
y=rand(10)
plot(x,y)
produces error:
ERROR: plot has no method matching plot(::Array(Float64,1),::Array(Float64,1))
I would be very grateful if someone coould explain to me why embeding the code within the plot line produces a result, but defining the variables beforehand results in an error. Logic says they should produce the same result.
I am using Julia v 0.3.1 and have loaded Gadfly as charting tool.
In the first case, you are using keyword argument syntax, not assigning to variables x and y (the meaning of = inside function calls is special). To get the same effect in the second case, you should use
x=rand(10)
y=rand(10)
plot(x=x,y=y)
which passes the value in the variable x in the keyword argument x to plot, and the value in the variable y in the keyword argument y.
In case you didn't. Write this before your code:
using plots
plyplot()
I am searching for a fast and simple way to give comprehensible names to the columns of a VAR-coefficient matrix.
What I would like to use is the function VAR.names, which is used in the function VAR.est() in the VAR.etp-package. When I use the function VAR.est(), this works perfectly, but as soon as I modify VAR.est (by adding another element to the list of values which are returned), I receive an error message stating "could not find function VAR.names".
I could not find any information on the function VAR.names.
Example:
library(VAR.etp)
data(dat)
M=VAR.est(dat,p=2,type="const")
M$coef
Another possibility would be to use a loop as in the function VAR() from the vars package, but if VAR.names would actually work, this would be a lot more elegant!
I am using integrate function (in R) to numerically compute integrals. I have an univariate function with one argument f(x,a) like this (just for example purpose):
test = function(x,a) 1/sqrt(2*pi)*exp(-(x-a)^2/2)
I want to define new univariate function, which is a function of a after integrate the above function:
testa = function(a) integrate(test,0,Inf,a=a)$value #this works
Now my question is that is it possible to use integrate function on function testa ? For example:
integrate(testa,0,1) # not working
I tried and it is not working (got error message evaluation of function gave a result of wrong length). I already know that one can apply multivariate integration procedure directly on test (for example use adaptIntegrate function from cubature package). But that is not my purpose!
So does anyone know how to apply successively integrate function like the example above? Or confirm that this is not permitted in R?
Thank in advance
integrate needs a vectorized function. You can use Vectorize:
integrate(Vectorize(testa),0,1)
#0.6843731905 with absolute error < 0.00000000000022
Disclaimer: I haven't checked the result for correctness.