Home > Mobile >  r solve linear equation
r solve linear equation

Time:07-11

This is my equation

k_0 = 0.21
k_1 = 0.21
m = 52

alpha = 0.05
beta  = 0.2
pi_0 = 0.669
pi_1 to be estimated
power <- 1-beta
cz <- 20
z_alpha <- qnorm(p= alpha/2, lower.tail=FALSE)
Z_beta <- qnorm(p= beta, lower.tail=FALSE)   

If this is my equation, how do I solve for pi or estimate pi given values for all other parameters ?

 cz  <- 1   ((z_alpha   Z_beta)^2)*((pi_0*(1-pi_0)/m  pi_1*(1- pi_1)/m   (((k_0)^2)*((pi_0)^2)   ((k_1)^2)*((pi_1)^2)))/((pi_0 - pi_1)^2))

CodePudding user response:

You can use the following code

library(minpack.lm)

k_0 = 0.21
k_1 = 0.21
m = 52

alpha = 0.05
beta  = 0.2
pi_0 = 0.669

power <- 1-beta
cz <- 20
z_alpha <- qnorm(p= alpha/2, lower.tail=FALSE)
Z_beta <- qnorm(p= beta, lower.tail=FALSE)

fun <- as.formula(cz  ~ 1   ((z_alpha   Z_beta)^2)*((pi_0*(1-pi_0)/m   pi_1*(1 - pi_1)/m   (((k_0)^2)*((pi_0)^2)   ((k_1)^2)*((pi_1)^2)))/((pi_0 - pi_1)^2)))
df <- data.frame(cz, z_alpha, Z_beta, alpha, m, beta, pi_0, k_0, k_1)

#Fitting model using minpack.lm package
nls.out <- nlsLM(fun, 
                  data = df,
                  start=list(pi_1=1),
                  algorithm = "LM",
                  control = nls.lm.control(maxiter = 500))

summary(nls.out)

#> Formula: cz ~ 1   ((z_alpha   Z_beta)^2) * ((pi_0 * (1 - pi_0)/m   pi_1 * 
#>    (1 - pi_1)/m   (((k_0)^2) * ((pi_0)^2)   ((k_1)^2) * ((pi_1)^2)))/((pi_0 - 
#>    pi_1)^2))
#>
#> Parameters:
#>      Estimate Std. Error t value Pr(>|t|)
#> pi_1   0.8219        NaN     NaN      NaN
#>
#> Residual standard error: NaN on 0 degrees of freedom
#>
#> Number of iterations to convergence: 7 
#> Achieved convergence tolerance: 1.49e-08

It could have been better if you have used series of values for dependent and independent variables. The constants may have single values. You have to tell which are the constants (i.e. parameters) in your equation.

  • Related