パネル データを分割し、データのパネルの性質を維持したいと思います。
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) が 2 つのデータセットに分割されることを回避したいと思います。データからランダムにサンプリング/分割し、観察ではなく対応するパーティションに個人を割り当てる方法です。 ?
私はサンプリングしようとしました:
mysample <- data[sample(unique(data$id), 1000,replace=FALSE),]
ただし、それはデータのパネルの性質を破壊します...