merge_factor: Merge factor levels

View source: R/ts.R

merge_factorR Documentation

Merge factor levels

Description

Merges factor levels with similar response distributions (assumed normal).

Usage

merge_factor(f, x, n, same.var = T, trace = T, xlab = NA, ylab = NA)

Arguments

f

a factor

x

a numerical vector, same length as f

n

the desired number of factor levels

same.var

argument passed to ward

trace

If TRUE, a merging trace is plotted (plot_hclust_trace)

xlab, ylab

axis labels. If NA, taken from f and x arguments.

Details

Calls ward(split(x,f)) to get a tree, cuts the tree, and constructs a new factor. The tree is shown via boxplot.hclust.

Value

A new factor, same length as f, but with n levels.

Author(s)

Tom Minka

Examples

n <- 20
x <- c(rnorm(n)+1, rnorm(n)+2, rnorm(n)*4+2)
f <- gl(3,n)
levels(f) <- c("a","b","c")
merge_factor(f,x,2,same.var=T)
merge_factor(f,x,2,same.var=F)

# an ordered factor
data(va.deaths)
merge_factor(va.deaths$Age,va.deaths$Rate,2)


paulemms/datamining documentation built on March 1, 2023, 4:01 p.m.