Skip to content

Commit f306793

Browse files
committed
closes #14 closes #17 and passes all the current version tests.
1 parent c651f84 commit f306793

File tree

6 files changed

+125
-55
lines changed

6 files changed

+125
-55
lines changed

R/cox.zph.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#' Test the proportional hazards assumption of an RPSFTM/Cox Regression
2+
#' @param fit the result of fitting a rpsftm model using \code{\link[survival]{coxph}} as the inner estimation tool.
3+
#' @param ... any other arguments to pass to \code{\link[survival]{cox.zph}}.
4+
#'
5+
#' @description If the the \code{fit} inherits *both* \code{rpsftm} and \code{coxph} then this pulls out the genuine
6+
#' \code{survival::coxph} object that is deeply nested in the object, and then runs \code{survival::cox.zph} on it.
7+
#' Or it avoids overwriting the function from \code{survival} by calling \code{survival::cox.zph} directly if the object
8+
#' does not inherit \code{rpsftm}. Or it fails.
9+
#'
10+
#' @note This does rely on the order of loading packages. The \code{rpsftm} package must be loaded after \code{survival},
11+
#' if both are required, to avoid the masking of synonymous functions causing errors.
12+
#'
13+
#' @seealso \code{\link[survival]{cox.zph}}
14+
#' @export
15+
16+
17+
cox.zph <- function(fit,...){
18+
if(inherits(fit, "rpsftm")){
19+
if( !inherits(fit, "coxph")){
20+
stop("This model did not use coxph within g-estimation")
21+
} else {
22+
fit <- attr(fit$ans$f.root,"fit")
23+
}
24+
}
25+
survival::cox.zph(fit,...=...)
26+
}

R/est_eqn.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
est_eqn <- function(psi, response, data, formula_list, treatment_matrix, rand_matrix, target = 0, test = "survdiff",
2525
autoswitch, ...) {
2626

27+
# ensure the psi is now always a matrix, with rows equal to n.
2728
if ("(treat_modifier)" %in% names(data)) {
28-
psi <- psi * data[, "(treat_modifier)"]
29-
}
29+
psi <- data[, "(treat_modifier)"] %*%diag(psi, length(psi), length(psi))
30+
} else{
31+
psi <- matrix(psi, nrow=nrow(treatment_matrix), ncol=length(psi), byrow=TRUE)
32+
}
3033

3134
#response <- model.response(data)
3235
#treatment_matrix <- model.matrix(treatment, data=data)

R/print.rpsftm.R

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,51 +43,52 @@ print.rpsftm <- function(x,...) {
4343
#' @keywords internal
4444

4545
print.coxph <- function (x, digits = max(options()$digits - 4, 3), ...){
46-
if (!is.null(x$fail)) {
47-
cat(" Coxph failed.", x$fail, "\n")
48-
return()
49-
}
50-
savedig <- options(digits = digits)
51-
on.exit(options(savedig))
52-
coef <- x$coefficients
53-
rand_names <- colnames(model.matrix(x$formula_list$randomise, data=x$data))
54-
rand_names <- rand_names[rand_names!="(Intercept)"]
55-
arm_index <- which(names(coef) %in% rand_names)
56-
coef <- coef[-arm_index, drop=FALSE]
57-
se <- sqrt(diag(x$var[-arm_index, -arm_index, drop=FALSE]))
58-
if (is.null(coef) | is.null(se))
59-
stop("Input is not valid")
60-
if (is.null(x$naive.var)) {
61-
tmp <- cbind(coef, exp(coef), se, coef/se, 1 - stats::pchisq((coef/se)^2,
62-
1))
63-
dimnames(tmp) <- list(names(coef), c("coef", "exp(coef)",
64-
"se(coef)", "z", "p"))
65-
}
66-
else {
67-
nse <- sqrt(diag(x$naive.var[-arm_index, -arm_index, drop=FALSE]))
68-
tmp <- cbind(coef, exp(coef), nse, se, coef/se, 1 - stats::pchisq((coef/se)^2,
46+
if (!is.null(x$fail)) {
47+
cat(" Coxph failed.", x$fail, "\n")
48+
return()
49+
}
50+
51+
savedig <- options(digits = digits)
52+
on.exit(options(savedig))
53+
coef <- x$coefficients
54+
if (is.null(coef)) stop("Input is not valid")
55+
rand_names <- colnames(model.matrix(x$formula_list$randomise, data=x$data))
56+
rand_names <- rand_names[rand_names!="(Intercept)"]
57+
arm_index <- which(names(coef) %in% rand_names)
58+
coef <- coef[-arm_index, drop=FALSE]
59+
se <- sqrt(diag(x$var[-arm_index, -arm_index, drop=FALSE]))
60+
if (is.null(se)) stop("Input is not valid")
61+
if (is.null(x$naive.var)) {
62+
tmp <- cbind(coef, exp(coef), se, coef/se, 1 - stats::pchisq((coef/se)^2,
6963
1))
70-
dimnames(tmp) <- list(names(coef), c("coef", "exp(coef)",
71-
"se(coef)", "robust se", "z", "p"))
72-
}
73-
if(length(coef)>0){
74-
stats::printCoefmat(tmp, signif.stars = FALSE, P.values = TRUE,
75-
has.Pvalue = TRUE)}
76-
#logtest <- -2 * (x$loglik[1] - x$loglik[2])
77-
#if (is.null(x$df))
78-
# df <- sum(!is.na(coef))
79-
#else df <- round(sum(x$df), 2)
80-
#cat("\n")
81-
#cat("Likelihood ratio test=", format(round(logtest, 2)),
82-
# " on ", df, " df,", " p=", format(1 - pchisq(logtest,
83-
# df)), "\n", sep = "")
84-
omit <- x$na.action
85-
cat("n=", x$n)
86-
if (!is.null(x$nevent)) cat(", number of events=", x$nevent)
87-
cat("\n")
88-
if (length(omit))
89-
cat(" (", stats::naprint(omit), ")\n", sep = "")
90-
invisible(x)
64+
dimnames(tmp) <- list(names(coef), c("coef", "exp(coef)",
65+
"se(coef)", "z", "p"))
66+
}
67+
else {
68+
nse <- sqrt(diag(x$naive.var[-arm_index, -arm_index, drop=FALSE]))
69+
tmp <- cbind(coef, exp(coef), nse, se, coef/se, 1 - stats::pchisq((coef/se)^2,
70+
1))
71+
dimnames(tmp) <- list(names(coef), c("coef", "exp(coef)",
72+
"se(coef)", "robust se", "z", "p"))
73+
}
74+
if(length(coef)>0){
75+
stats::printCoefmat(tmp, signif.stars = FALSE, P.values = TRUE,
76+
has.Pvalue = TRUE)}
77+
#logtest <- -2 * (x$loglik[1] - x$loglik[2])
78+
#if (is.null(x$df))
79+
# df <- sum(!is.na(coef))
80+
#else df <- round(sum(x$df), 2)
81+
#cat("\n")
82+
#cat("Likelihood ratio test=", format(round(logtest, 2)),
83+
# " on ", df, " df,", " p=", format(1 - pchisq(logtest,
84+
# df)), "\n", sep = "")
85+
omit <- x$na.action
86+
cat("n=", x$n)
87+
if (!is.null(x$nevent)) cat(", number of events=", x$nevent)
88+
cat("\n")
89+
if (length(omit))
90+
cat(" (", stats::naprint(omit), ")\n", sep = "")
91+
invisible(x)
9192
}
9293

9394
#'modified version of print.survreg

R/untreated.R

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,26 @@ untreated <- function(psi, response,treatment_matrix, rand_matrix, censor_time,
2525

2626

2727
nontreatment <- 1-rowSums(treatment_matrix) #apply(treatment_matrix,1,sum)
28-
# won't work with treatment_modifier at present!!!
29-
#treatment_matrix <- sweep(treatment_matrix,2, exp(psi), FUN="*")
30-
#treatment <- rowSums(treatment_matrix)#apply(treatment_matrix,1, sum)
31-
treatment <- treatment_matrix %*% exp(psi)
28+
treatment <- rowSums(treatment_matrix*exp(psi))
3229

3330
u <- time * (nontreatment + treatment)
3431

3532

3633
#make use of setting censor_time=Inf to avoid recensoring, and implimenting Autoswitch
3734

38-
c_star <- censor_time* min(1, exp(psi) ) #apply(cbind( censor_time , censor_time %o% exp(psi)),1, min)
39-
if( autoswitch){
35+
36+
37+
c_star <- censor_time* apply(cbind(1, exp(psi)),1, min)#min(1, exp(psi) ) #apply(cbind( censor_time , censor_time %o% exp(psi)),1, min)
38+
if( autoswitch & ncol(treatment_matrix)==1){
39+
check <- aggregate( treatment_matrix[,1], by=list(rand_matrix[,-1]), FUN=sd)
40+
switch_values <- check[check[,2]==0,1]
41+
c_star <- ifelse( rand_matrix[,-1] %in% switch_values, Inf, c_star)
42+
4043
#if( all(rx[arm==1]==1)){ c_star <- ifelse(arm==1, Inf, c_star)}
4144
#if( all(rx[arm==0]==0)){ c_star <- ifelse(arm==0, Inf, c_star)}
4245
}
4346

44-
t_star <- (u < c_star)*(u-c_star) + c_star #pmin(u, c_star)
47+
t_star <- pmin(u, c_star) # doesn't work if c_star=Inf : (u < c_star)*(u-c_star) + c_star
4548
#only change delta if necessary
4649
delta_star <- (u < c_star)*delta #ifelse( c_star < u, 0, delta)
4750
output <- cbind("time"=t_star, "status"=delta_star)

build_dashboard.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
rcmdcheck::rcmdcheck()
3+
4+
5+
devtools::build_vignettes(clean=FALSE)
6+
devtools::clean_vignettes()
7+
8+
9+
10+
.libPaths("U:/My Documents/R/win-library/3.6")
11+
devtools::build_vignettes()
12+
devtools::document(roclets = c('rd', 'collate', 'namespace'))
13+
devtools::build()
14+
15+
#Check you've got the right version number
16+
install.packages("../rpsftm_1.2.6.tar.gz",
17+
repos = NULL, type = "source")
18+
devtools::build("../rpsftm_1.2.6.tar.gz", binary=TRUE)
19+
20+
install.packages("V:/STATISTICS/NON STUDY FOLDER/Software/R/R Code Library/cctu_0.9.0.zip",
21+
repos = NULL, type="win.binary")
22+
23+
#devtools::build( binary=TRUE)
24+
# modify news.md, cran-comments.md README.rmd
25+
devtools::check_win_release()
26+
rcmdcheck::rcmdcheck()
27+
devtools::check_win_devel()
28+
devtools::check_rhub()
29+
spelling::spell_check_package()
30+
#check github is totally up to date
31+
devtools::release()
32+
devtools::submit_cran()
33+
#add a tag to github
34+
#do some publicity , edit the website news, CTU stats email list.
35+
#Add the .9000 suffix to the Version field in the DESCRIPTION to
36+
#indicate that this is a development version.
37+
#Create a new heading in NEWS.md and commit the changes.

tests/testthat/test_rpsftm.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ context("Test the rpsftm() function")
66

77
test_that("check of test that with mixed data sources",{
88
propX <- with(immdef,I(1-xoyrs/progyrs))
9-
fit <- lm(progyrs~propX, data=immdef)
10-
expect_is(fit$coefficients, "numeric")
9+
fit <- rpsftm(Surv(progyrs,prog)~rand(propX~imm), data=immdef,censyrs)
10+
expect_is(fit$psi, "numeric")
1111
})
1212

1313
test_that("first basict fit",{

0 commit comments

Comments
 (0)