wald_test
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
wald_test [2023/12/04 14:00] – created hkimscil | wald_test [2023/12/07 23:51] (current) – [Wald test in logistic regression] hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Wald Test ====== | ====== Wald Test ====== | ||
Regression model의 coefficient값이 significant 한지 테스트하는 방법. 즉, regression coefficient의 t-test와 비슷한 일을 한다. | Regression model의 coefficient값이 significant 한지 테스트하는 방법. 즉, regression coefficient의 t-test와 비슷한 일을 한다. | ||
+ | |||
+ | H0: Some set of predictor variables are all equal to zero. | ||
+ | HA: Not all predictor variables in the set are equal to zero. | ||
+ | |||
+ | < | ||
+ | #fit regression model | ||
+ | model <- lm(mpg ~ disp + carb + hp + cyl, data = mtcars) | ||
+ | |||
+ | #view model summary | ||
+ | summary(model) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = mpg ~ disp + carb + hp + cyl, data = mtcars) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -5.0761 -1.5752 -0.2051 | ||
+ | |||
+ | Coefficients: | ||
+ | | ||
+ | (Intercept) 34.021595 | ||
+ | disp -0.026906 | ||
+ | carb -0.926863 | ||
+ | hp | ||
+ | cyl | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 2.973 on 27 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | # coefficient | ||
+ | coef(model) | ||
+ | |||
+ | | ||
+ | 34.021594525 -0.026906182 -0.926863291 | ||
+ | |||
+ | # term1, 2, 3, 4, 5 | ||
+ | install.packages(" | ||
+ | library(aod) | ||
+ | |||
+ | # wald.test(Sigma, | ||
+ | #perform Wald Test to determine if 3rd and 4th predictor variables are both zero | ||
+ | wald.test(Sigma = vcov(model), | ||
+ | |||
+ | Wald test: | ||
+ | ---------- | ||
+ | |||
+ | Chi-squared test: | ||
+ | X2 = 3.6, df = 2, P(> X2) = 0.16 | ||
+ | |||
+ | </ | ||
+ | |||
+ | wald.test(Sigma, | ||
+ | * Sigma: The variance-covariance matrix of the regression model | ||
+ | * b: A vector of regression coefficients from the model | ||
+ | * Terms: A vector that specifies which coefficients to test | ||
+ | |||
+ | ====== Wald test in logistic regression ====== | ||
+ | < | ||
+ | odds <- function(p) | ||
+ | odds.ratio <- function(p1, | ||
+ | logit <- function(p) | ||
+ | ilogit | ||
+ | |||
+ | iter <- 10000 | ||
+ | n <- 350 | ||
+ | p.cancer <- 0.08 | ||
+ | p.mutant <- 0.39 | ||
+ | |||
+ | logor <- rep (NA, iter) | ||
+ | pp0 <- rep (NA, iter) | ||
+ | pp1 <- rep (NA, iter) | ||
+ | op0 <- rep (NA, iter) | ||
+ | op1 <- rep (NA, iter) | ||
+ | or <- rep (NA, iter) | ||
+ | |||
+ | for(i in 1:iter){ | ||
+ | c <- runif(n, 0, 1) | ||
+ | canc <- ifelse(c> | ||
+ | c <- runif(n, 0, 1) | ||
+ | gene <- ifelse(c> | ||
+ | | ||
+ | da <- data.frame(gene, | ||
+ | tab <- table(da) | ||
+ | pp0[i] <- tab[1,1] / (tab[1,1] + tab[1,2]) | ||
+ | pp1[i] <- tab[2,1] / (tab[2,1] + tab[2,2]) | ||
+ | op0[i] <- odds(pp0[i]) | ||
+ | op1[i] <- odds(pp1[i]) | ||
+ | or[i] <- odds.ratio(pp0[i], | ||
+ | # stats <- c(pp0, pp1, op0, op1, ortemp) | ||
+ | logor[i] <- log(or[i]) | ||
+ | } | ||
+ | hist(logor, | ||
+ | |||
+ | |||
+ | </ | ||
wald_test.1701666030.txt.gz · Last modified: 2023/12/04 14:00 by hkimscil