Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

preprocessor - preProcess function R

I'm very new to R.

I'm trying to follow the procedure below:

imputedData <- preProcess( select(train, -SalePrice),
  method = c("center", "scale", "knnImpute", "nzv", 'YeoJohnson')
  )
#install.packages('RANN')
library(RANN)
trainTrans <- predict(imputedData, train)

And I have this error

Must subset rows with a valid subscript vector. x Subscript nn$nn.idx must be a simple vector, not a matrix.

I have already install Caret package

The train dataset is a table that I have imported from a CSV file

question from:https://stackoverflow.com/questions/66066126/preprocess-function-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the program

Loading of packages

knitr::opts_chunk$set(echo = TRUE, cache = TRUE, message = FALSE, warning = FALSE)

library(tidyverse)
library(caret)
library(GGally)
library(lattice)
library(corrplot)
library(factoextra)
library(FactoMineR)
library(magrittr)
theme_set(theme_bw())
set.seed(181019)

Data loading

train <- readr::read_csv("train.csv")
test  <- readr::read_csv("test.csv")

Missing value

missing_threshold <- .4
is_too_scarce <- lapply(select(train, -SalePrice), function(x) mean(is.na(x)) > missing_threshold)
is_too_scarce <- map_lgl(select(train, -SalePrice), ~mean(is.na(.x)) > missing_threshold)
not_too_scarce <- names(is_too_scarce)[!is_too_scarce]
train <- select(train, SalePrice, not_too_scarce)
train %<>% select(SalePrice, not_too_scarce)
test  %<>% select(not_too_scarce)

Preprocessing

imputedData <- preProcess( select(train, -SalePrice),
  method = c("center", "scale", "knnImpute", "nzv", 'YeoJohnson')
  )
#install.packages('RANN')
library(RANN)
testTrans  <- predict(imputedData, test)
trainTrans <- predict(imputedData, train)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...