Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在移除响应变量进行标准化后,如何将其重新合并到数据帧中?_R_Standardization - Fatal编程技术网

R 在移除响应变量进行标准化后,如何将其重新合并到数据帧中?

R 在移除响应变量进行标准化后,如何将其重新合并到数据帧中?,r,standardization,R,Standardization,我有一个61列的数据集(60个解释变量和1个响应变量) 所有解释变量都是数字,响应是分类的(默认)。一些外部变量有负值(财务数据),因此标准化似乎比规范化更明智。但是,当使用“apply”函数进行标准化时,我必须首先删除响应变量,因此我会: model如果要将列model$Default添加到modelSTAN数据框中,可以这样做 # assign the column directly modelSTAN$Default <- model$Default # or use cbind f

我有一个61列的数据集(60个解释变量和1个响应变量)

所有解释变量都是数字,响应是分类的(默认)。一些外部变量有负值(财务数据),因此标准化似乎比规范化更明智。但是,当使用“apply”函数进行标准化时,我必须首先删除响应变量,因此我会:


model如果要将列
model$Default
添加到
modelSTAN
数据框中,可以这样做

# assign the column directly
modelSTAN$Default <- model$Default
# or use cbind for columns (rbind is for rows)
modelSTAN <- cbind(modelSTAN, model$Default)
请注意,在
dplyr
版本中,我没有费心保存原始的means和SDs,如果以后要反向转换,仍然应该这样做。默认情况下,
scale
将使用
mean
sd

# assign the column directly
modelSTAN$Default <- model$Default
# or use cbind for columns (rbind is for rows)
modelSTAN <- cbind(modelSTAN, model$Default)
modelSTAN <- model 
## get index of response, here named default
resp <- which(names(modelSTAN) == "default")
## standardize all the non-response columns
means <- colMeans(modelSTAN[-resp])
sds <- apply(modelSTAN[-resp], 2, sd)
modelSTAN[-resp] <- scale(modelSTAN[-resp], center = means, scale = sds)
library(dplyr)
modelSTAN <- model %>%
  mutate(across(-all_of("default"), scale))