R-dummy-R生成哑变量的三种方法

本文介绍了使用R的caret包创建哑变量的两种方法,包括dummyVars函数的运用,以及对比了base R的model.matrix函数。通过示例展示了如何从earth包的etitanic数据集中处理pclass和sex因素,生成完整的哑变量,但注意到这种参数化可能不适用于所有模型函数,如lm。
library("nnet")
	dummy_income <- class.ind(hairPre$income)
	head(dummy_income) #Display the head 6 lines
	class(hairPre$income)
	#Do not have to as.factor() the variable
library(dummies)
	#to create dummies for specific variable
	#Do not have to as.factor() the variable
	d1 <- hairPre
	d1 <- cbind(d1, dummy(d1$income, sep = "."))
	#dummy.data.frame() call for the function model.matrix()
	class(d1$income)
	names(d1)
#Have to as.factor() the variable first
	hairPre$income <- as.factor(hairPre$income)
	dum_income <- model.matrix(~income, hairPre)
	class(hairPre$income)
	head(cbind(dum_income, hairPre$income))

caret包可以很简便地生成dummies
两种方式:

The function dummyVars can be used to generate a complete (less than full rank parameterized) set of dummy variables from one or more factors. The function takes a formula and a data set and outputs an object that can be used to create the dummy variables using the predict method.

For example, the etitanic data set in the earth package includes two factors: pclass (passenger class, with levels 1st, 2nd, 3rd) and sex (with levels female, male).

The base R function model.matrix would generate the following variables:

library(earth)
data(etitanic)
head(model.matrix(survived ~ ., data = etitanic))

Using dummyVars:

dummies <- dummyVars(survived ~ ., data = etitanic)
head(predict(dummies, newdata = etitanic))

Note there is no intercept and each factor has a dummy variable for each level, so this parameterization may not be useful for some model functions, such as lm.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值