0

Good morning, all!

I am attempting to output 10 examples of test data that has been run through my model. Admittedly, I am trying to use someone else's code that I adapted to my needs. However, the output I am getting is not what they got, and I cannot figure out why.

Here is the code as I adapted it:

s = sample( test[["mpg"]])
pred <- predict(model,newdata = autoMPG[s,])
z <- autoMPG[["mpg"]][s]

for ( i in 1:2 ) {
  print( paste( i,s,z, "=", pred[1:10]))
}

The output is supposed to be a list like this:

[1] "1 1526 5 = 5.54867084076152"  
[1] "2 413 5 = 4.95790890809035"  
[1] "3 133 5 = 6.15451009369984"  
[1] "4 1062 8 = 6.53549803946163"  
[1] "5 1564 5 = 5.36176753883042"  
[1] "6 396 7 = 6.56540500470723"  
[1] "7 37 6 = 5.62346051846786"  
[1] "8 1295 6 = 5.7652546426557"  
[1] "9 208 5 = 4.99319541968862"  
[1] "10 1398 5 = 5.13480728298929"  

What I am instead getting is a list where i=1 is repeated for all 98 objects in s, and then again for i=2, and so on.

[1] "1 26 10 = 8.08808398965549"   "1 26 10 = 8.08808398965549"   "1 13 15 = 13.1237890047814"    
 [4] "1 24 26 = 23.4755796610819"   "1 32 25 = 24.4615684249838"   "1 21.5 25 = 21.2212989104033"  
 [7] "1 11 15 = 14.4796068968617"   "1 16 22 = 19.924438447419"    "1 33.5 19 = 21.6845378530998"  
[10] "1 26 10 = 8.08808398965549"   "1 38 14 = 11.2628565963676"   "1 17.6 18 = 20.3102676949862"  
[13] "1 14 14 = 17.5989382712609"   "1 34.1 16 = 16.7252629171779" "1 18 21 = 21.594151320571"     
[16] "1 20 26 = 27.0608492137994"   "1 26 10 = 8.08808398965549"   "1 31.9 28 = 24.4091504746601"  
[19] "1 27.9 10 = 9.77302160656119" "1 23 25 = 23.1635241057839"   "1 13 15 = 13.1237890047814"    
[22] "1 18 21 = 21.594151320571"    "1 16 22 = 19.924438447419"    "1 20.5 26 = 27.0608492137994"  
[25] "1 14 14 = 17.5989382712609"   "1 27.5 10 = 9.77302160656119" "1 18 21 = 21.594151320571"     
[28] "1 32 25 = 24.4615684249838"   "1 14 14 = 17.5989382712609"   "1 11 15 = 14.4796068968617"    
[31] "1 27 10 = 9.77302160656119"   "1 20.8 26 = 27.0608492137994" "1 24 26 = 23.4755796610819"    
[34] "1 36 19 = 17.6211715089865"   "1 13 15 = 13.1237890047814"   "1 20 26 = 27.0608492137994"    
[37] "1 14 14 = 17.5989382712609"   "1 27 10 = 9.77302160656119"   "1 15 24 = 22.878137066301"     
[40] "1 29 9 = 7.8719748297333"     "1 24 26 = 23.4755796610819"   "1 34.2 16 = 16.7252629171779"  
[43] "1 25 21 = 21.0732409251392"   "1 19 27 = 24.3996898340437"   "1 25 21 = 21.0732409251392"...

Any suggestions as to what might be wrong, or even a better way to do this, would be greatly appreciated. TIA!

*Edited: I added the wrong line of code before.

  • You are using `s` as a sample of `mpg`, so it produces a rearrangement of all the numbers in `mpg`. But you are also using `s` to subset `mpg`, which doesn't make any sense. When you put numbers in the square brackets, these have to be the _indexes_ that you want to subset. – Allan Cameron Mar 23 '20 at 15:15
  • @AllanCameron Thanks. As I said, I borrowed this code from somewhere else, and it seemed to work there. The original code was this: `trn = sample( 1:length(wine[["quality"]]), 0.9 * length(wine[["quality"]] ) ) tst = setdiff( 1:length(wine[["quality"]]), trn ) mod = opt_model( wine[trn,], "quality" ) for ( i in 1:10 ) { s = sample( tst, 1 ) print( paste( i,s,wine[["quality"]][s],"=",predict(mod$Model, newdata = wine[s,]) )) }` – Discombobul8d Mar 23 '20 at 15:32
  • Now, I did use different code to set up my train and test datasets. Also, I had to add 'unlist' because of an error I was getting. Other than that, all I did was substitute my set names. Any idea why their code worked and mine won't? – Discombobul8d Mar 23 '20 at 15:35

0 Answers0