friendly / twoway

Analysis of Two-Way Tables a la Tukey
https://friendly.github.io/twoway/
4 stars 1 forks source link

more flexible #5

Open friendly opened 4 years ago

friendly commented 4 years ago

At present, twoway(x, ...) relies on x being a matrix or data.frame with the row labels being row.names(x).

we should allow a row.names = arg, to use a column in a data.frame or tibble, and remove this from the data.

rmheiberger commented 4 years ago

withNames <- function(x, rn=rownames(x), cn=colnames(x), nm=deparse(substitute(x)), rN=attr(x, "response"), vN=names(dimnames(x))) { if (is.null(rn)) rownames(x) <- letters[1:nrow(x)] if (is.null(cn)) colnames(x) <- LETTERS[1:ncol(x)] if (is.null(rN)) attr(x, "response") <- "Value" if (length(names(dimnames(x))) < 2) { if (is.null(vN)) varNames <- c("Row", "Col") else { ## exactly 1 na.varNames <- is.na(vN) vN[na.varNames] <- c("Row", "Col")[na.varNames] varNames <- vN } names(dimnames(x)) <- varNames } x }

twoway.default <- function(x, method=c("mean", "median"), ..., name=deparse(substitute(x)), responseName=attr(x, "response"), varNames=names(dimnames(x)), rownames=rownames(x), colnames=colnames(x)) { result <- switch(match.arg(method), mean=meanfit(x, ...), median=medianfit(x, ...)) result$name <- name result$varNames <- varNames result$responseName <- responseName result$compValue <- with(result, matrix(roweff) %*% (coleff/overall)) ## rmh dimnames(result$compValue) <- dimnames(result$residuals) result$slope <- with(result, unname(coef(lm(c(residuals) ~ c(compValue)))[2])) result$power <- 1 - result$slope result }

tmp <- matrix(1:12, 4, 3) tmp withNames(tmp) twoway(withNames(tmp))

friendly commented 4 years ago

OK, this is good. But I was thinking of a more common case where a data.frame is read with it's first column being row names.

> VermontPop <- as.data.frame(read_excel("VermontPop.xlsx"))
> 
> str(VermontPop)
'data.frame':   14 obs. of  11 variables:
 $ County: chr  "Chittenden" "Rutland" "Washington" "Windsor" ...
 $ 1990  : num  5120 4793 4740 4733 4602 ...
 $ 1980  : num  5063 4766 4719 4708 4541 ...
 $ 1970  : num  4996 4721 4678 4644 4495 ...
 $ 1960  : num  4872 4669 4632 4628 4469 ...
 $ 1950  : num  4796 4662 4632 4612 4476 ...
 $ 1940  : num  4717 4659 4619 4578 4471 ...
 $ 1930  : num  4676 4685 4620 4573 4477 ...
 $ 1920  : num  4641 4665 4590 4568 4477 ...
 $ 1910  : num  4628 4682 4620 4527 4475 ...
 $ 1900  : num  4598 4646 4564 4508 4480 ...

Here, manually, I would do:

# make first column row.names
row.names(VermontPop) <- VermontPop[,1]
VermontPop <- VermontPop[,-1]

twoway(VermontPop)

But, that case might be handled internally via recognizing rownames=1 as a special case.

rmheiberger commented 4 years ago

VermontPop <- data.frame(VermontPop[,-1], row.names=VermontPop[,1])

which is essentially the same as what you suggest.

But more importantly, this type of transformation belongs to any function that takes data.frame arguments

and shouldn't be limited to just twoway.

On Fri, Jul 3, 2020 at 7:12 PM Michael Friendly notifications@github.com wrote:

OK, this is good. But I was thinking of a more common case where a data.frame is read with it's first column being row names.

VermontPop <- as.data.frame(read_excel("VermontPop.xlsx"))

str(VermontPop) 'data.frame': 14 obs. of 11 variables: $ County: chr "Chittenden" "Rutland" "Washington" "Windsor" ... $ 1990 : num 5120 4793 4740 4733 4602 ... $ 1980 : num 5063 4766 4719 4708 4541 ... $ 1970 : num 4996 4721 4678 4644 4495 ... $ 1960 : num 4872 4669 4632 4628 4469 ... $ 1950 : num 4796 4662 4632 4612 4476 ... $ 1940 : num 4717 4659 4619 4578 4471 ... $ 1930 : num 4676 4685 4620 4573 4477 ... $ 1920 : num 4641 4665 4590 4568 4477 ... $ 1910 : num 4628 4682 4620 4527 4475 ... $ 1900 : num 4598 4646 4564 4508 4480 ...

Here, manually, I would do:

make first column row.names

row.names(VermontPop) <- VermontPop[,1] VermontPop <- VermontPop[,-1]

twoway(VermontPop)

But, that case might be handled internally via recognizing rownames=1 as a special case.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/friendly/twoway/issues/5#issuecomment-653693406, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAP5ZI5AS4LZLMTBVQMXMV3RZZQT7ANCNFSM4OP5I36Q .