Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started