我想对面板数据进行分区并保留数据的面板性质:
library(caret)
library(mlbench)
#example panel data where id is the persons identifier over years
data <- read.table("http://people.stern.nyu.edu/wgreene/Econometrics/healthcare.csv",
header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
## Here for instance the dependent variable is working
inTrain <- createDataPartition(y = data$WORKING, p = .75,list = FALSE)
# subset into training
training <- data[ inTrain,]
# subset into testing
testing <- data[-inTrain,]
# Here we see some intersections of identifiers
str(training$id[10:20])
str(testing$id)
但是我想,在对数据进行分区或采样时,避免将同一个人(id)分成两个数据集。他们是一种从数据中随机采样/分区的方法,将个体分配给相应的分区而不是观察?
我尝试采样:
mysample <- data[sample(unique(data$id), 1000,replace=FALSE),]
然而,这破坏了数据的面板性质......