Target value and for loop in R -


some data:

a <- function(a) {3*a+12*a^2} dom1 <- seq(-1,4,0.1) vec1 <- a(dom1)[1:10]  c <- function(c) {-5*c^2+2*c^3} dom2 <- seq(-0.1,0.2,0.01) vec2 <- c(dom2)[1:10]  d <- function(d) {2*d^2+5*d^3+12*d^4} dom3 <- seq(0.1,0.5,0.01) vec3 <- d(dom3)[1:10]  w <- function(w) {7*w-3*w^2} dom4 <- seq(0.5,2.5,0.05) vec4 <- w(dom4)[1:10] 

now suppose fit lm model on larger data set lm(y~a+c+d+w) , lm parameters c(-0.2,0.2,0.1,0.6)

fun.mean <- function(a,c,d,w) {-0.2*a+0.2*c+0.1*d+0.6*w} 

what tried, doesn't work expected:

here loop through vector generated relevant function (&domains)

for(a in vec1) {  for(c in vec2) {   for(d in vec3) {    for(w in vec4) {     sol <- fun.mean(a,c,d,w)     if (sol %% 1 > 0.40 & sol < 0.50) print(c(a,c,d,w))   }}}} 

so i'm looking find combinations of c(a,c,d,w), equal 0.5 or ideally equal interval 0.4-0.5.

so multiplying c(a,c,d,w) output function fun.mean not give desired values (interval 0.4-0.5). i'm doing wrong? there better approach find c(a,c,d,w) values given "target" value? alternative for loop slow.

you filter using %%1 don't use when check answer. following code returns 9.00 -0.052000 0.02620000 2.7500 gives -0.15778. matches criteria (-0.15778 %% 1 = 0.85 > 0.4 , -0.15 < 0.5).

i guess criteria wrong then, should either

sol %% 1 > 0.4 , sol %% 1 < 0.5 

or

sol > 0.4 , sol < 0.5. 

in first case, maybe should add %% 1 in fun.mean calculation.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -