kmeans: data.frame-first formula-second method for 'kmeans'

Description Usage Arguments See Also Examples

Description

This function passes a data.frame, formula, and additional arguments to kmeans.

Usage

1
2
3
4
kmeans(data, formula = ~., ...)

## S3 method for class 'kmeans'
predict(object, data, ...)

Arguments

data

Data frame (or object that can be coerced to one) containing the variables in the model

formula

A formula (or object that can be coerced to one) with no response variable (optional). If omitted, all variables in "data" will be included.

...

Additional arguments to pass to model function

object

Fitted model

See Also

kmeans

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Standard kmeans
kmeans(iris, centers = 3)

# formula interface can be used to select certain variables
kmeans(iris, ~ Petal.Width + Sepal.Width, centers = 3)

#... or create new variables. Consider non-linear example:

# Create data for two circles
x <- matrix(rnorm(300),nc=2)
y <- x/sqrt(rowSums(x^2))
d <- data.frame(rbind(y, y*5))
plot(d)

fit <- kmeans(d, centers = 2)
plot(d, col = predict(fit, d))

fit <- kmeans(d, ~ X1 + X2 + I(X1^2 + X2^2), centers = 2)
plot(d, col = predict(fit, d))

drsimonj/twidlr documentation built on May 15, 2019, 2:53 p.m.