简体   繁体   English

r 求解线性方程

[英]r solve linear equation

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 ?如果这是我的方程,我如何求解 pi 或估计 pi ​​给定所有其他参数的值?

 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))

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 (ie parameters) in your equation.您必须说出方程式中的常数(即参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM