Missing argument in a function - r

I have an ODE function LVM() which takes the arguments time,population, carry capacity and growth rate. I need to compute its stability by finding the Jacobian using 'pracma' R package or 'rootSolve' package but i get an error
Error in fun(x, ...) : argument "r" is missing, with no default
my function is;
LVM <- function(t,pop,int_mat,str_mat,carry_cap,r){
dN=r*pop*((carry_cap-(int_mat*str_mat)%*%pop)/carry_cap)
return(dN)
}
When i compute the Jacobian as below;
jacobian(LVM,c(0.287778,0.2553485,0.147619,0.3661074,0.4390739,0.1270218,0.4533318,0.2236241,0.3555208,0.2102366))
where the numerical values are population densities i get an error
Error in fun(x, ...) : argument "r" is missing, with no default
Your input will be highly appreciated.

With a modified function LVM_V2 which takes a vector of parameters instead
of 6 parameters, I get the following output. Is this the expected output
Mapping of parameters:
#r=>x[6]
#pop => x[2]
#carry_cap => x[5]
#int_mat => x[3]
#str_mat => x[4]
LVM_V2 <- function(x){
dN=x[6]*x[2]*((x[5]-(x[3]*x[4])%*%x[2])/x[5])
return(dN)
}
pracma::jacobian(LVM_V2,c(0.287778,0.2553485,0.147619,0.3661074,0.4390739,0.1270218,0.4533318,0.2236241,0.3555208,0.2102366))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 0 0.1190372 -0.006905828 -0.002784515 0.002321776 0.2473229 0 0 0 0
Note that the parameter t remains unused in your function

Related

maximize the value of a function using a for loop

I'm a bit new to R and I'm trying to maximize a simple value function, for a given parameter space.
The idea is to solve for c given different values of a.
The function is
2*(c^2)-(7.8*c)-(4*c*a)+(5*a)+(a^2)+6=0
And I would like to solve for the different values of c, given values of a from 0 to 100.
Is there any way to do this simply using a for loop?
Thanks a lot!
The equation can be solved analytically for c (since it's a simple degree 2 polynomial equation). Using e.g. Wolfram Alpha gives the two solutions
We can roll this up into a function to calculate c for different a
func_c <- function(a) a + 1/20 * (39 + c(-1, 1) * sqrt(200 * a^2 + 560 * a + 321))
sapply(0:10, func_c)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
#[1,] 1.054176 1.306072 1.58304 1.867387 2.154937 2.44417 2.734398 3.025264 3.316562 3.608168 3.9
#[2,] 2.845824 4.593928 6.31696 8.032613 9.745063 11.45583 13.165602 14.874736 16.583438 18.291832 20.0
If you must use a numerical root finder (and I don't recommend doing this here since this has a simple closed-form analytical solution) you can use polyroot. The coefficients can be read off from the equation you give
pol_coef <- function(a) c(5 * a + a^2 + 6, 7.8 + 4 * a, 2)
sapply(0:10, \(x) abs(polyroot(pol_coef(x))))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
#[1,] 1.054176 1.306072 1.58304 1.867387 2.154937 2.44417 2.734398 3.025264 3.316562 3.608168 3.9
#[2,] 2.845824 4.593928 6.31696 8.032613 9.745063 11.45583 13.165602 14.874736 16.583438 18.291832 20.0
Solutions from both approaches are identical.

Count all values in a correlation matrix that are above 0.8 and below -0.8

I have a matrix of 2134 by 2134 of correlation values and I would like to count the total number of values that are above 0.8 and below -0.8. I have tried
length(TFcoTF[TFcoTF>.8])
but this does not seem to be correct as I am getting about 50 percent of values above .8 which does not correspond to the histogram I have for the data. Also when I do
length(TFcoTF[TFcoTF<-.8])
I got 0 as the output. Any help is appreciated.
The data table package has a function called between. This returns TRUE/FALSE value for each value in your matrix whether the value is between two values.
In my example below, I randomly created a 10x10 matrix with random values [-1,+1]. Using the length function and subsetting where the values are in your range of [-0.8,+0.8].
library(data.table)
data <- matrix(runif(100,-1,1), nrow = 10, ncol=10)
data
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0.05585901 -0.7497720 -0.8371569 -0.401079424 -0.4130752 -0.788961736 0.2909987 0.48965177 0.4076504 -0.0682856
[2,] -0.42442920 0.7476111 0.8238973 -0.912507391 -0.4450897 -0.001308901 0.5151425 -0.16838841 -0.1648151 0.8370660
[3,] -0.73295874 0.5271986 0.5822628 -0.008554908 -0.2785803 -0.499058508 -0.5661172 0.35957967 0.5807055 0.2350893
[4,] 0.18949338 0.3827603 -0.6112584 0.209209240 -0.5883962 -0.087900052 0.1272227 0.58165922 -0.9950324 -0.9118599
[5,] 0.40862973 0.9496163 0.4996253 0.079538601 0.9839763 -0.119883751 0.3667418 -0.02751815 -0.6724141 0.3217434
[6,] 0.77338548 -0.7698167 -0.5632436 0.223301216 -0.9936610 0.650110638 -0.9400395 -0.47808065 -0.1579283 -0.6896787
[7,] 0.93210326 0.5360980 0.7677325 0.815231731 -0.4320206 0.647954028 0.5180600 -0.09574138 -0.3848389 0.9726445
[8,] -0.66411834 0.1125759 -0.4021577 -0.711363103 0.7161801 -0.071971464 0.7953436 0.40326575 0.6895480 0.7496597
[9,] 0.14118154 0.4775983 0.8966069 0.852880293 0.4715885 -0.542526148 0.5200246 -0.62649677 -0.3677738 0.1961003
[10,] -0.59353193 -0.2358892 0.5769562 -0.287113142 -0.7100862 -0.107092848 -0.8101459 -0.46754146 -0.4082147 -0.4475972
length(data[between(data,-0.8,0.8)])
[1] 84
It's difficult to answer without having your dataset, please provide a minimal reproducible example later.
For the first line of code, this looks correct.
For the second, the error comes from a syntax error. In R you can assign value with = and <-. So x<-1 assign the value whereas x < -1 return a boolean.
You can then combine logical values and run the code below :
set.seed(42)
m <- matrix(runif(25, min = -1, max = 1), nrow = 5, ncol = 5)
m
length(m[ m > .8]) + length(m[ m < -.8]) # long version from what you did.
length(m[ m < -.8 | m > .8]) # | mean or. TRUE | FALSE will return TRUE.
sum(m > .8 | m < -.8)
# The sum of logical is the length, since sum(c(TRUE, FALSE)) is sum(c(0, 1))
sum(abs(m) > .8) # is the shortest version

WRS package in R - mulrank and cmanova

When using the mulrank function from the WRS package I get a zero p-value
$test.stat
[1] 44.50749
$nu1
[1] 3.330729
$p.value
[,1]
[1,] 0
$N
[1] 98
$q.hat
[,1] [,2] [,3]
[1,] 0.4260204 0.6738095 0.6554422
[2,] 0.6619048 0.1530612 0.1530612
[3,] 0.4974490 0.5928571 0.6323129
Is this a reasonable output?
Also, when using the cmanova function, I get this error message on the same data:
Error in X[i, ] - X[ii, ] : non-numeric argument to binary operator

Replicate each time with different standard deviation

I have a vector of standard deviations:
sd_vec<-runif(10,0,20) with 10 values between 0 and 20.
[1] 11.658106 9.693493 12.695608 4.091922 5.761061 18.410951 14.710990 12.095944 18.023123
[10] 13.294963
I would like to replicate the following process:
a<-rnorm(10,0,30)
[1] -21.265083 85.557147 23.958170 -32.843328 6.629831 -23.745339 46.094324 51.020059
[9] 1.041724 13.757235
n_columns=50
replicate(n_columns, a+rnorm(length(a), mean=0,sd=sd_vec))
The result should be 10 columns each of which are:
column 1: a + rnorm(length(a),0,11.658106)
column 2: a + rnorm(length(a),0,9.693493)
column 3: a + rnorm(length(a),0,12.695608)
.
.
.
column 10:a + rnorm(length(a),0,13.294963)
Will this use different values of sd_vec for each replication or will it use it for each random number generation?
According to your edit, then you may want to try
a+sapply(sd_vec, rnorm, n=100, mean=0)
# example
> set.seed(1)
> sd_vec <-runif(10,0,20)
> set.seed(1)
> a<-rnorm(100,0,30)
> n_columns=10
> head(a+sapply(sd_vec, rnorm, n=100, mean=0))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] -22.087869 -15.746650 -8.554735 0.7226986 -18.481801 -24.921835 -32.16206 -33.158153 -38.187974
[2,] 5.732942 18.078702 -6.489666 39.9422684 4.311839 32.504554 42.75921 -18.624133 7.954302
[3,] -29.906010 -13.260709 -2.483113 -36.0217953 -29.841630 -15.576334 -26.76925 -11.915258 -21.741820
[4,] 48.697584 45.395650 43.463125 40.7586401 47.903975 57.600406 47.59359 47.701659 33.782184
[5,] 6.409275 -7.122582 28.836887 2.3249113 13.884993 7.429514 -11.34081 1.960571 18.075706
[6,] -15.229450 -6.025260 -7.288529 -31.4375515 -18.184563 -45.038651 -50.00938 -26.965804 -37.610292
[,10]
[1,] -17.391109
[2,] 6.883342
[3,] -26.144900
[4,] 48.118830
[5,] 9.970987
[6,] -26.668629
Your current solution will replicate sd_vec for each replication, not using each sd for each replication.
If you want to have columns for each sd then you may work on matrices. Create matrix of rnorm with desire sd by:
X <- rnorm(length(a)*n_columns, mean=0, sd=sd_vec)
X <- matrix(X, nrow=length(a), ncol=n_columns, byrow=TRUE)
Then add it to a converted to matrix:
matrix(a, nrow=length(a), ncol=n_columns) + X

R hdf5 dataset written incorrectly?

When I execute the following my "predictors" dataset is populated correctly:
library(rhdf5)
library(forecast)
library(sltl)
library(tseries)
fid <- H5Fcreate(output_file)
## TODO: compute the order p
p <- 4
# write predictors
h5createDataset(output_file, dataset="predictors", c(p, length(tsstl.remainder) - (p - 1)), storage.mode='double')
predictors <- as.matrix(tsstl.remainder)
for (i in 1:(p - 1)) {
predictors <- as.matrix(cbind(predictors, Lag(as.matrix(tsstl.remainder), i)))
}
predictors <- as.matrix(predictors[-1:-(p-1),])
head(predictors)
h5write(predictors, output_file, name="predictors")
H5Fclose(fid)
The generated (correct) output for head(predictors) is:
[,1] [,2] [,3] [,4]
[1,] 0.3089645 6.7722063 5.1895389 5.2323261
[2,] 8.7607228 0.3089645 6.7722063 5.1895389
[3,] -0.9411553 8.7607228 0.3089645 6.7722063
[4,] -14.1390243 -0.9411553 8.7607228 0.3089645
[5,] -26.6605296 -14.1390243 -0.9411553 8.7607228
[6,] -8.1293076 -26.6605296 -14.1390243 -0.9411553
However, when I read it the results are not correct:
tsmatrix <- t(as.matrix(h5read(output_file, "predictors")))
head(tsmatrix)
Incorrectly outputs:
[,1] [,2] [,3] [,4]
[1,] 0.3089645 8.760723 -0.9411553 -14.13902
[2,] -26.6605296 -8.129308 -9.8687675 31.52086
[3,] 54.2703126 43.902489 31.8164836 43.87957
[4,] 22.1260636 36.733055 54.7064107 56.35158
[5,] 36.3919851 25.193068 48.2244464 57.12196
[6,] 48.0585673 72.402673 68.3265518 80.18960
How come what I write does not correspond to what I get back? I double-checked and hdfview HDF5 viewer also shows this incorrect values for the "predictors" dataset.
What is wrong here?
From the rhdf5 docs:
Please note, that arrays appear as transposed matrices when opening it
with a C-program (h5dump or HDFView). This is due to the fact the
fastest changing dimension on C is the last one, but on R it is the
first one (as in Fortran).

Resources