Question: I got the error: Error in yall [ test ] : invalid subscript type 'list' Traceback: 1 . mean ( ( kmod$fitted - yall [
I got the error:
Error in yalltest: invalid subscript type 'list'
Traceback:
meankmod$fitted yalltest
Below is my code:
## import libraries needed
librarytestthat
librarydigest
librarystringr
libraryMASS
librarykknn
n nrowBoston #number of observatonsrows ie sample size
xall Boston$dis #our input variable
yall Boston$nox #our output variable
# create data frame df that consists only of our input and output variables
df data.framexallyall #simple data frame for convenience
dimdf #checking the dimension of the dataframe df note there are obs
set.seed
tr samplex: size replaceFALSE
headtr
# Create train and test data frames
train dftr
test dftr
#loop over values of k fit on train, predict on test
kvec :
nklengthkvec
outRMSE repnk #preallocating RMSE for outofsample data
inRMSE repnk #preallocating RMSE for insample data
# Loop over the values of k
for i in kvec
# Fit the kknn model using the training dataset
kmod kknnyall ~ xall, traintrain, testtest, k kernel"rectangular"
kmodtr kknnyall ~ xall, traintrain, testtrain, k kernel"rectangular"
# Compute the outofsample and insample RMSEs
outRMSEi sqrtmeankmod$fitted yalltest
inRMSEi sqrtmeankmodtr$fitted yalltrain
# Print the first few values of the outofsample and insample RMSEs
printoutRMSE
printinRMSE
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
