Hi
I am trying to run the below code but not sure why I am getting Nan as result. Please can someone advise what the issue is?
BreastCancer_scaled=data.frame(
Cl.thickness = c(0.197759762794049, 0.197759762794049,
-0.511268678089602, 0.552273983235875,
-0.156754457647776, 1.26130242411953, -1.22029711897325,
-0.865782898531427, -0.865782898531427, -0.156754457647776,
-1.22029711897325, -0.865782898531427, 0.197759762794049,
-1.22029711897325, 1.26130242411953, 0.9067882036777, -0.156754457647776,
-0.156754457647776, 1.97033086500318, 0.552273983235875,
0.9067882036777, 1.97033086500318, -0.511268678089602,
-1.22029711897325, 0.197759762794049, -0.511268678089602,
0.197759762794049, -0.865782898531427, -1.22029711897325,
-0.511268678089602, -0.865782898531427, 1.97033086500318,
-0.865782898531427, -0.511268678089602, -0.865782898531427,
1.97033086500318, 0.552273983235875, 0.197759762794049,
-0.865782898531427, 1.97033086500318, 0.552273983235875,
0.197759762794049, 1.97033086500318, -1.22029711897325,
-0.511268678089602, -1.22029711897325, -0.156754457647776,
0.9067882036777, 1.61581664456135, 0.197759762794049),
Cell.size = c(-0.701697757221419, 0.277048808160941,
-0.701697757221419, 1.58204422867075,
-0.701697757221419, 2.23454193892566, -0.701697757221419,
-0.701697757221419, -0.701697757221419, -0.375448902093965,
-0.701697757221419, -0.701697757221419, -0.049200046966512,
-0.701697757221419, 1.2557953735433, 0.277048808160941,
-0.701697757221419, -0.701697757221419, 1.2557953735433,
-0.701697757221419, -0.049200046966512, 0.603297663288395,
-0.701697757221419, -0.701697757221419, -0.375448902093965,
-0.375448902093965, -0.701697757221419, -0.701697757221419,
-0.701697757221419, -0.701697757221419, -0.701697757221419,
1.2557953735433, -0.701697757221419, -0.701697757221419,
-0.701697757221419, 2.23454193892566, -0.375448902093965,
0.277048808160941, 0.603297663288395, 0.277048808160941,
2.23454193892566, 0.929546518415848, 2.23454193892566,
-0.701697757221419, 1.2557953735433, -0.701697757221419,
-0.701697757221419, 1.58204422867075, 0.603297663288395,
-0.049200046966512),
Cell.shape = c(-0.74123039483746, 0.262590543048829, -0.74123039483746,
1.60101846023055, -0.74123039483746, 2.27023241882141,
-0.74123039483746, -0.40662341554203, -0.74123039483746,
-0.74123039483746, -0.74123039483746, -0.74123039483746,
-0.0720164362466006, -0.74123039483746, 0.597197522344259,
0.931804501639689, -0.74123039483746, -0.74123039483746,
1.26641148093512, -0.74123039483746, -0.40662341554203,
0.597197522344259, -0.74123039483746, -0.74123039483746,
-0.0720164362466006, -0.74123039483746, -0.74123039483746,
-0.74123039483746, -0.0720164362466006, -0.74123039483746,
-0.74123039483746, 1.26641148093512, -0.74123039483746,
-0.40662341554203, -0.74123039483746, 2.27023241882141,
-0.74123039483746, 0.262590543048829, -0.0720164362466006,
-0.0720164362466006, 2.27023241882141, 0.597197522344259,
2.27023241882141, -0.74123039483746, 1.26641148093512,
-0.74123039483746, -0.74123039483746, 1.26641148093512,
1.60101846023055, -0.0720164362466006),
Marg.adhesion = c(-0.638897301750389, 0.757476640955261,
-0.638897301750389, -0.638897301750389,
0.0592896696024361, 1.8047570979845, -0.638897301750389,
-0.638897301750389, -0.638897301750389, -0.638897301750389,
-0.638897301750389, -0.638897301750389, 0.0592896696024361,
-0.638897301750389, 2.50294406933732, 0.408383155278848,
-0.638897301750389, -0.638897301750389, 1.10657012663167,
-0.638897301750389, 2.50294406933732, 0.0592896696024361,
-0.638897301750389, -0.638897301750389, 0.408383155278848,
-0.638897301750389, -0.638897301750389, -0.638897301750389,
-0.638897301750389, -0.638897301750389, -0.638897301750389,
0.0592896696024361, -0.289803816073976, -0.638897301750389,
-0.638897301750389, 1.8047570979845, -0.638897301750389,
2.15385058366091, 0.0592896696024361, -0.638897301750389,
-0.289803816073976, 1.10657012663167, 0.408383155278848,
-0.638897301750389, 0.408383155278848, -0.638897301750389,
0.0592896696024361, -0.289803816073976, -0.638897301750389,
0.408383155278848),
Epith.c.size = c(-0.555201605659206, 1.69392470908123,
-0.555201605659206, -0.105376342711119,
-0.555201605659206, 1.69392470908123, -0.555201605659206,
-0.555201605659206, -0.555201605659206, -0.555201605659206,
-1.00502686860729, -0.555201605659206, -0.555201605659206,
-0.555201605659206, 1.69392470908123, 1.24409944613314,
-0.555201605659206, -0.555201605659206, 0.344448920236969,
-0.555201605659206, 0.794274183185057, 1.24409944613314,
-0.555201605659206, -0.555201605659206, -0.555201605659206,
-1.00502686860729, -0.555201605659206, -0.555201605659206,
-0.555201605659206, -1.00502686860729, -0.555201605659206,
2.14374997202932, -0.555201605659206, -0.555201605659206,
-0.555201605659206, 1.24409944613314, -1.00502686860729,
-0.555201605659206, 1.24409944613314, -0.105376342711119,
2.14374997202932, 3.0434004979255, 2.14374997202932,
-0.555201605659206, 0.344448920236969, -0.555201605659206,
-0.555201605659206, 0.344448920236969, -0.555201605659206,
-0.555201605659206),
Bare.nuclei = c(-0.698341295402917, 1.77156891336678,
-0.423906827761839, 0.124962107520315,
-0.698341295402917, 1.77156891336678, 1.77156891336678, -0.698341295402917,
-0.698341295402917, -0.698341295402917,
-0.698341295402917, -0.698341295402917, -0.149472360120762,
-0.149472360120762, 1.4971344457257, -0.698341295402917,
-0.698341295402917, -0.698341295402917, 1.77156891336678,
-0.698341295402917, 1.77156891336678, 0.948265510443546,
-0.698341295402917, -0.698341295402917, 0.948265510443546,
-0.698341295402917, -0.698341295402917, -0.698341295402917,
-0.698341295402917, -0.698341295402917, -0.698341295402917,
0.399396575161392, -0.698341295402917, -0.698341295402917,
-0.698341295402917, -0.698341295402917, -0.698341295402917,
1.77156891336678, 0.948265510443546, -0.149472360120762,
1.77156891336678, -0.698341295402917, -0.698341295402917,
-0.698341295402917, 1.4971344457257, -0.698341295402917,
-0.698341295402917, 1.22269997808462, -0.149472360120762,
0.124962107520315),
Bl.cromatin = c(-0.181693999725951, -0.181693999725951,
-0.181693999725951, -0.181693999725951,
-0.181693999725951, 2.26758893079032, -0.181693999725951,
-0.181693999725951, -0.998121643231376, -0.589907821478663,
-0.181693999725951, -0.589907821478663, 0.226519822026761,
-0.181693999725951, 0.634733643779474, 0.226519822026761,
-0.589907821478663, -0.181693999725951, 0.226519822026761,
-0.181693999725951, 0.634733643779474, 1.4511612872849,
-0.589907821478663, -0.181693999725951, -0.181693999725951,
-0.589907821478663, -0.589907821478663, -0.589907821478663,
-0.998121643231376, -0.589907821478663, -0.181693999725951,
1.4511612872849, -0.181693999725951, -0.589907821478663,
-0.589907821478663, 1.85937510903761, 1.4511612872849,
0.634733643779474, 1.4511612872849, 1.04294746553219, 1.4511612872849,
-0.181693999725951, 1.85937510903761, -0.589907821478663,
0.226519822026761, -0.589907821478663,
-0.181693999725951, -0.181693999725951, -0.589907821478663,
-0.181693999725951),
Normal.nucleoli = c(-0.612478497036736, -0.284896027595788,
-0.612478497036736, 1.35301631960895,
-0.612478497036736, 1.35301631960895, -0.612478497036736,
-0.612478497036736, -0.612478497036736, -0.612478497036736,
-0.612478497036736, -0.612478497036736, 0.370268911286108,
-0.612478497036736, 0.697851380727057, 0.0426864418451602,
-0.612478497036736, -0.612478497036736, -0.612478497036736,
-0.612478497036736, 0.370268911286108, 2.3357637279318,
-0.612478497036736, -0.612478497036736, 1.025433850168,
-0.612478497036736, -0.612478497036736, -0.612478497036736,
-0.612478497036736, -0.612478497036736, -0.612478497036736,
0.370268911286108, -0.612478497036736, -0.612478497036736,
-0.612478497036736, 2.00818125849085, -0.612478497036736,
1.025433850168, 0.697851380727057, 0.697851380727057,
0.0426864418451602, -0.612478497036736, 2.3357637279318,
-0.612478497036736, 1.6805987890499, -0.612478497036736,
-0.612478497036736, 1.6805987890499, -0.612478497036736,
0.370268911286108),
Mitoses = c(-0.348144562975438, -0.348144562975438,
-0.348144562975438, -0.348144562975438,
-0.348144562975438, -0.348144562975438, -0.348144562975438,
-0.348144562975438, 1.96042569442479, -0.348144562975438,
-0.348144562975438, -0.348144562975438, -0.348144562975438,
-0.348144562975438, 1.38328313007474, -0.348144562975438,
-0.348144562975438, -0.348144562975438, 0.22899800137462,
-0.348144562975438, 1.38328313007474, -0.348144562975438,
-0.348144562975438, -0.348144562975438, -0.348144562975438,
-0.348144562975438, -0.348144562975438, -0.348144562975438,
-0.348144562975438, -0.348144562975438, -0.348144562975438,
0.806140565724679, -0.348144562975438, -0.348144562975438,
-0.348144562975438, -0.348144562975438, -0.348144562975438,
-0.348144562975438, -0.348144562975438, 0.22899800137462,
0.806140565724679, -0.348144562975438, -0.348144562975438,
0.22899800137462, -0.348144562975438, -0.348144562975438,
-0.348144562975438, 0.22899800137462, 1.96042569442479,
-0.348144562975438),
y = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,
0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1,
1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1)
)
## 10-fold cross validation
nfolds = 10
## Sample fold assignment index
fold_index = sample(nfolds, n, replace=TRUE)
#> Error in sample.int(x, size, replace, prob): object 'n' not found
fold_sizes = numeric(nfolds)
for (k in 1:nfolds) fold_sizes[k] = length(which(fold_index == k))
#> Error in which(fold_index == k): object 'fold_index' not found
## Create vector to hold average MSE computed over each fold
cv_subset_errors = numeric(nfolds)
for (k in 1:nfolds) {
# Fit model by least squares using all but k-th fold
subset_tmp_fit = lm(y ~ Cl.thickness+Cell.shape+Marg.adhesion+Bare.nuclei+Bl.cromatin+Normal.nucleoli,data=BreastCancer_scaled[fold_index!=k,])
# Compute fitted values for the k-th fold
subset_tmp_predict = predict(subset_tmp_fit, BreastCancer_scaled[fold_index==k,])
# Work out the MSE for the k-th fold
cv_subset_errors[k] = mean((BreastCancer_scaled[fold_index==k,]$lcrim - subset_tmp_predict)^2)
}
#> Error in is.data.frame(data): object 'BreastCancer_scaled' not found
cv_subset_errors
#> [1] 0 0 0 0 0 0 0 0 0 0
## Compute average MSE over entires data set - NOTE: need to weight results
## by number of values in each fold!!!
subset_final_mse = weighted.mean(cv_subset_errors, w=fold_sizes)
subset_final_mse
#> [1] NaN
Created on 2019-11-30 by the reprex package (v0.3.0)