How to create a command to choose linearization alternatives

Asked

Viewed 36 times

3

I have the following command to obtain coefficients for linearization of the Michaelis Menten function.

# "Estimativas para "CHUTE" 

# 1 para (Eadie-Hofstee  y = -B(y/x) + A)(Especialmente para RMSEA com valores nulos)
# 2 para (x = A(x/y) - B)
# 3 para (Hanes-Woolf  x/y = x/A + B/A)
# 4 para (Lineweaver-Burk  1/y = (B/Ax) + 1/A)

if(ch==1){

  #Alternativa 1   (Eadie-Hofstee  y = -B(y/x) + A)

  dados_Chute <- data.frame(Y=c(Indice),x=c(Indice)/(rep(c(pp), each=100)))   
  options(na.action=na.exclude)
  ajuste<-lm(Y~x,data=dados_Chute)
  A <- (ajuste[["coefficients"]][["(Intercept)"]])
  B <- -(ajuste[["coefficients"]][["x"]])
  C <- A
} else if(ch==2) {

  # Alternativa 2   (x = A(x/y) - B)

  dados_Chute <- data.frame(Y=(rep(c(pp), each=100)),x=(rep(c(pp), each=100))/(c(Indice)))  
  options(na.action=na.exclude)
  ajuste<-lm(Y~x,data=dados_Chute)
  A <- ajuste[["coefficients"]][["x"]]
  B <- -(ajuste[["coefficients"]][["(Intercept)"]])
  C <- 1 - A
} else if(ch==3){

  # Alternativa 3   (Hanes-Woolf  x/y = x/A + B/A)

  dados_Chute <- data.frame(Y = (rep(c(pp), each=100))/c(Indice),x = (rep(c(pp), each=100)))  
  options(na.action=na.exclude)
  ajuste<-lm(Y~x,data=dados_Chute)
  A <- 1/(ajuste[["coefficients"]][["x"]])
  B <- (ajuste[["coefficients"]][["(Intercept)"]])*A
  C <- 1 - A
} else if(ch==4){

  # Alternativa 4   (Lineweaver-Burk  1/y = (B/Ax) + 1/A)

  dados_Chute <- data.frame(Y = 1/c(Indice),x = 1/(rep(c(pp), each=100)))  
  options(na.action=na.exclude)
  ajuste<-lm(Y~x,data=dados_Chute)
  A <- 1/(ajuste[["coefficients"]][["(Intercept)"]])
  B <- (ajuste[["coefficients"]][["x"]])*A
  C <- 1 - A
} else {
  print ("Erro!")
}

I need to create a command in which the program user can choose between one of the alternatives to estimate the coefficients (linearization), from the choice of the value of 'ch', without having to repeat the whole command each time you want to run one of the alternatives. I repeat this 4 times during my program (one for each calculated index (RMSEA, GFI, NFI and CFI). I am adjusting a curve the estimates calculated for several values of observations and sometimes I need to choose one of the alternatives for linearization (it is not always the same for all indices).

  • You tried to put your code inside a function that depends on ch ? Maybe I didn’t quite understand what you wanted.

  • I even thought about your alternative, but you can have "if" and "if Else" inside a function??

  • Yes, you can pack if else within a function and when calling the function you define which method you want to use.

1 answer

3

An option would be to create a function with the option ch:

estimativa <- function(ch)
{
    # testes
    if(!ch %in% c(1:4)) stop("'ch' deve conter um valor entre 1 e 4")

    # "Estimativas para "CHUTE" 

    # 1 para (Eadie-Hofstee  y = -B(y/x) + A)(Especialmente para RMSEA com valores nulos)
    # 2 para (x = A(x/y) - B)
    # 3 para (Hanes-Woolf  x/y = x/A + B/A)
    # 4 para (Lineweaver-Burk  1/y = (B/Ax) + 1/A)

    if(ch==1){

      #Alternativa 1   (Eadie-Hofstee  y = -B(y/x) + A)

      dados_Chute <- data.frame(Y=c(Indice),x=c(Indice)/(rep(c(pp), each=100)))   
      options(na.action=na.exclude)
      ajuste<-lm(Y~x,data=dados_Chute)
      A <- (ajuste[["coefficients"]][["(Intercept)"]])
      B <- -(ajuste[["coefficients"]][["x"]])
      C <- A
    } else if(ch==2) {

      # Alternativa 2   (x = A(x/y) - B)

      dados_Chute <- data.frame(Y=(rep(c(pp), each=100)),x=(rep(c(pp), each=100))/(c(Indice)))  
      options(na.action=na.exclude)
      ajuste<-lm(Y~x,data=dados_Chute)
      A <- ajuste[["coefficients"]][["x"]]
      B <- -(ajuste[["coefficients"]][["(Intercept)"]])
      C <- 1 - A
    } else if(ch==3){

      # Alternativa 3   (Hanes-Woolf  x/y = x/A + B/A)

      dados_Chute <- data.frame(Y = (rep(c(pp), each=100))/c(Indice),x = (rep(c(pp), each=100)))  
      options(na.action=na.exclude)
      ajuste<-lm(Y~x,data=dados_Chute)
      A <- 1/(ajuste[["coefficients"]][["x"]])
      B <- (ajuste[["coefficients"]][["(Intercept)"]])*A
      C <- 1 - A
    } else if(ch==4){

      # Alternativa 4   (Lineweaver-Burk  1/y = (B/Ax) + 1/A)

      dados_Chute <- data.frame(Y = 1/c(Indice),x = 1/(rep(c(pp), each=100)))  
      options(na.action=na.exclude)
      ajuste<-lm(Y~x,data=dados_Chute)
      A <- 1/(ajuste[["coefficients"]][["(Intercept)"]])
      B <- (ajuste[["coefficients"]][["x"]])*A
      C <- 1 - A
    }

    return(c(A, B, C))
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.