Hi
I have a issue with R Markdown were the values of my regression keep changing when i knitt as PDF or try to re-run the chunk I have set seed on each chunk. Here is a sample of my work. If someone can advise me where I am going wrong i would be very grateful. Many Thanks
CancerData=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)
)
X1orig = CancerData[,1:9]
X1 = scale(X1orig)
## Pick out response variable
y = CancerData[,10]
p = ncol(CancerData) - 1
## Fit model to full data
library(glmnet)
set.seed(1)
## Choose grid of values for the tuning parameter
grid = 10 ^ seq(-3, 0, length.out = 100)
ridge = glmnet(
as.matrix(CancerData[1:9]),
#BreastCancer[,10],
y,
alpha = 0,
standardize = FALSE,
lambda = grid
)
## Show how coefficients vary with the tuning parameter
plot(ridge,
xvar = "lambda",
col = rainbow(p),
label = TRUE)