1

I looked at the the R code here (How to compute Variability Independent of the Mean (VIM)) but have run into my own trouble trying to calculate variability independent of the mean.

I have included my code where I am trying to repurpose it. I removed the set seed and made my own mini df with 3 sample subjects and blood pressures to work with.

library(matrixStats)

df <- data.frame( "ID" = c("ID1", "ID2", "ID3"), "x1" = c(120, 130, 140), "x2" = c(120, 135, 145), "x3" = c(121, 150, 155))

#compute row average df$avg <- rowMeans(df[2:4])

#compute row standard deviation df$std <- rowSds(as.matrix(df[2:4]))

#tune parameters for VIM nls.vim = nls(std ~ k*avg^p, data=df, start=c(k=1,p=0)) summary(nls.vim)

#create scatterplot with overlay plot(std ~ avg, data=df) r = range(df$avg) x.new = seq(r[1], r[2],length.out = 1000) lines(x.new, predict(nls.vim,list(avg = x.new)), col='red', lty=2)

#VIM = k(std/avg)^p df$vim <- k(df$sntd/df$avg)^p

I don't understand how to set the p and k values for the non-linear regression part of the code. I am running into two issues dependent on if I set p to 0 or 1 (which was suggested in the prior comment thread)

Error in nls(std ~ k * avg^p, data = df, start = c(k = 1, p = 0)) : step factor 0.000488281 reduced below 'minFactor' of 0.000976562

Error in nls(std ~ k * avg^p, data = df2, start = c(k = 1, p = 1)) : singular gradient

Thank you!

  • Part of the problem is that none of the solutions given in that thread is really any good. The underlying ideas and techniques are over 50 years old and well known in the EDA literature as a spread vs level plot. Use lm to fit a line to log(sd) against log(mean). (This method makes little sense if any of the means are non-positive.) It will succeed without requiring you to guess good starting values. You don't "set" $p$ and $k:$ they are the results of the fit. – whuber Jun 14 '22 at 21:13

0 Answers0