[R] Adjusted survival curves

2017-10-06 Thread Beginner

For adjusted survival curves I took the sample code from here: 
https://rpubs.com/daspringate/survival
and adapted for my date, but got error.
I would like to understand what is my mistake. Thanks!

#ADAPTATION FOR MY DATA
library(survival)
library(survminer)
df<-read.csv("F:/R/data/base.csv", header = TRUE, sep = ";")
head(df)
ID start stop censor sex age stage treatment
1 1 0 66 0 2 1 3 1
2 2 0 18 0 1 2 4 2
3 3 0 43 1 2 3 3 1
4 4 0 47 1 2 3 NA 2
5 5 0 26 0 1 4 3 NA

S <- Surv(
time = df$start, 
time2 = df$stop, 
event = df$censor)
head(S)
[1] (0,66+] (0,18+] (0,43] (0,47] (0,26+] (0,29+]

model <- coxph(S ~ df$treatment + df$age + df$sex + df$stage, data = df)

plot(survfit(model), 
las=1,
xscale = 1.00,
xlab = "Months after diagnosis",
ylab = "Proportion survived",
main = "Baseline Hazard Curve")

# BEFORE NOW everything works, but then ERROR
treat <- with(colon,
data.frame(
treatment = levels(df$treatment),
age = rep(levels(df$age)[1], 2),
sex = rep(levels(df$sex)[1], 2),
stage = rep(levels(df$stage)[1], 2)))

str(treat)
'data.frame': 0 obs. of 0 variables
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to create a dendrogram with colored branches

2013-08-03 Thread beginner
Hi

I would like to create a dendrogram in R which has colored branches, like
the one shown below. 
 

So far I used following commands to create a standard dendrogram:


d <- dist(as.matrix(data[,29]))   # find distance matrix 
 hc <- hclust(d)# apply hirarchical clustering 
 plot(hc,labels=data[,1], main="", xlab="") # plot the dendrogram

How should I modify this code to obtain a desired result ? 

Thanks in advance for your help.




--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-create-a-dendrogram-with-colored-branches-tp4672993.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Selection of regressors

2013-08-24 Thread beginner
I have a question about the package leaps which I am using for model
selection.

I would like to compare 4 different selection methods: forward, backward,
stepwise and best subset. I used the code below:

library(leaps) forward <- regsubsets(Response ~.,data = mydata, method =
"forward", nbest=1)
backward <- regsubsets(Response ~.,data = mydata, method = "backward",
nbest=1) stepwise <- regsubsets(Response ~., data = mydata, method =
"seqrep", nbest=1) best subset <- regsubsets(Response ~.,data = mydata,
method = "forward", nbest=1)


opt = par (mfrow =c(2,2)) plot(forward, scale = "adjr2", main = "Forward
Selection") plot(backward, scale = "adjr2", main = "Backward Selection")
plot(stepwise, scale = "adjr2", main = "Stepwise selection") plot(best
subset, scale = "adjr2", main = "Best subset selection")

Using these commands I obtained figures below:
 

I am wondering why figure A and D are similar to each other (and also figure
B aand C). I would expect different algorithms to select models in a
different way. For instance models selected with forward selection method
should be chosen based on the significance level/ AIC value. On the other
hand models selected with best subset selection method should be chosen
based on the sample satisitcs. I am also wondering why forward selection
does not choose one variable at the time adding it to the exisitng model ?
Also Fig B shows that backward selection starts with eight variables in the
model. Why it does not start with all the variables and excludes one at the
time ?

I would be very grateful for these clarifications.



--
View this message in context: 
http://r.789695.n4.nabble.com/Selection-of-regressors-tp4674451.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to do an external validation with R

2013-05-18 Thread beginner
I would like to do external validation using R software. So far I have used
packages like "Design" and "DAAG". However they perform internal validation
rather than external one. In order to perform external validation I would
have to split my data beforehand into training and test set, leave the test
set on the site and use only the training set to select a model. I would
then test the selected model with the sample set left initially on the site.
I would like to repeat this process several times to make sure that all the
samples are included at least once in a test set. I thought that I need to
use a loop function in R to perform this process automatically. As I am new
to R I don't know how to make a loop. Could you please help me with this or
suggest an R package ? I would be very very grateful for help with this task
!



--
View this message in context: 
http://r.789695.n4.nabble.com/how-to-do-an-external-validation-with-R-tp4667404.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to write a loop in R to select multiple regression model and validate it ?

2013-06-04 Thread beginner
I would like to run a loop in R. I have never done this before, so I would be
very grateful for your help !

1. I have a sample set: 25 objects. I would like to draw 1 object from it
and use it as a test set for my future external validation. The remaining 24
objects I would like to use as a training set (to select a model). I would
like to repeat this process until all 25 objects are used as a test set. 

2. For each of the training sets I would like to run the following code:


library(leaps)
forward <- regsubsets(Y ~.,data = training, method = "forward", nbest=1) 
backward <- regsubsets(Y ~.,data = training, method = "backward", nbest=1)
stepwise <- regsubsets(Y ~., data = training, method = "seqrep", nbest=1)
exhaustive <- regsubsets(Y ~.,data = training, method = "forward", nbest=1)
summary(forward)
summary(backward)
summary(stepwise)
summary(exhaustive)

I would like R programme to select the best model (with the highest adjusted
R2) using each of the selection methods, so there are 4 final best models
(e.g. the best model selected with forward selection, the best model
selected with backward selection and so on...). 

 
Afterwards I would like to perform internal cross validation of all 4
selected models and choose 1 out of 4 which has the lowest average mean
squared error (MSE). I used to do it using the code below:

library(DAAG)
val.daag<-CVlm(df=training, m=1, form.lm=formula(Y ~ X1+X2+X3))
val.daag<-CVlm(df=training, m=1, form.lm=formula(Y ~ X1+X2+X4))
val.daag<-CVlm(df=training, m=1, form.lm=formula(Y ~ X3+X4+X5))
val.daag<-CVlm(df=training, m=1, form.lm=formula(Y ~ X4+X5+X7))

For the best selected model (the lowest MSE) I would like to perform an
external validation on 1 object left on the site at the beginning of the
study (please refer to point 1.).

3. And loop again using different training and test set 


I hope that you could help me with this. 

If you have any suggestions how to select the best model and perform
validation more efficiently, I would be happy to hear about that.

Thank you !




--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-write-a-loop-in-R-to-select-multiple-regression-model-and-validate-it-tp4668668.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to write a loop in R to select multiple regression model and validate it ?

2013-06-04 Thread beginner
I would like to run a loop in R. I have never done this before, so I would be
very grateful for your help !

1. I have a sample set: 25 objects. I would like to draw 1 object from it
and use it as a test set for my future external validation. The remaining 24
objects I would like to use as a training set (to select a model). I would
like to repeat this process until all 25 objects are used as a test set. 

2. For each of the training sets I would like to run the following code:


library(leaps)
forward <- regsubsets(Y ~.,data = training, method = "forward", nbest=1) 
backward <- regsubsets(Y ~.,data = training, method = "backward", nbest=1)
stepwise <- regsubsets(Y ~., data = training, method = "seqrep", nbest=1)
exhaustive <- regsubsets(Y ~.,data = training, method = "forward", nbest=1)
summary(forward)
summary(backward)
summary(stepwise)
summary(exhaustive)

I would like R programme to select the best model (with the highest adjusted
R2) using each of the selection methods, so there are 4 final best models
(e.g. the best model selected with forward selection, the best model
selected with backward selection and so on...). 

 
Afterwards I would like to perform internal cross validation of all 4
selected models and choose 1 out of 4 which has the lowest average mean
squared error (MSE). I used to do it using the code below:

library(DAAG)
val.daag<-CVlm(df=training, m=1, form.lm=formula(Y ~ X1+X2+X3))
val.daag<-CVlm(df=training, m=1, form.lm=formula(Y ~ X1+X2+X4))
val.daag<-CVlm(df=training, m=1, form.lm=formula(Y ~ X3+X4+X5))
val.daag<-CVlm(df=training, m=1, form.lm=formula(Y ~ X4+X5+X7))

For the best selected model (the lowest MSE) I would like to perform an
external validation on 1 object left on the site at the beginning of the
study (please refer to point 1.).

3. And loop again using different training and test set 


I hope that you could help me with this. 

If you have any suggestions how to select the best model and perform
validation more efficiently, I would be happy to hear about that.

Thank you !



--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-write-a-loop-in-R-to-select-multiple-regression-model-and-validate-it-tp4668669.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to write a loop in R to select multiple regression model and validate it ?

2013-06-05 Thread beginner
This is not a homework but part of my research. 



--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-write-a-loop-in-R-to-select-multiple-regression-model-and-validate-it-tp4668669p4668720.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Coefficients paths - comparison of ridge, lasso and elastic net regression

2013-06-05 Thread beginner
I would like to compare models selected with ridge, lasso and elastic net.
Fig. below shows coefficients paths using all 3 methods: ridge (Fig A,
alpha=0), lasso (Fig B; alpha=1) and elastic net (Fig C; alpha=0.5). The
optimal solution depends on the selected value of lambda, which is chosen
based on cross validation.

 

When looking at these plots, I would expect the elastic net (Fig C) to
exhibit a grouping effect. However it is not clear in the presented case.
The coefficients path for lasso and elastic net are very similar. What could
be the reason for this ? Is it just a coding mistake ? I used the following
code in R:


library(glmnet)
X<- as.matrix(mydata[,2:22])
Y<- mydata[,23]
par(mfrow=c(1,3))
ans1<-cv.glmnet(X, Y, alpha=0) # ridge
plot(ans1$glmnet.fit, "lambda", label=FALSE)
text (6, 0.4, "A", cex=1.8, font=1)
ans2<-cv.glmnet(X, Y, alpha=1) # lasso
plot(ans2$glmnet.fit, "lambda", label=FALSE)
text (-0.8, 0.48, "B", cex=1.8, font=1)
ans3<-cv.glmnet(X, Y, alpha=0.5) # elastic net 
plot(ans3$glmnet.fit, "lambda", label=FALSE)
text (0, 0.62, "C", cex=1.8, font=1)


The code used to plot elastic net coefficients paths is exactly the same as
for ridge and lasso. The only difference is in the value of alpha. Alpha
parameter for elastic net regression was selected based on the lowest MSE
(mean squared error) for corresponding lambda values.

Thank you for your help !



--
View this message in context: 
http://r.789695.n4.nabble.com/Coefficients-paths-comparison-of-ridge-lasso-and-elastic-net-regression-tp4668722.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Scatter plot with error bars

2013-06-27 Thread beginner
Hi

I would like to plot multiple data sets on a scatter plot with error bars.
To do this I write the following code:

install.packages("Hmisc")
library(Hmisc)

x1<-data1[,1]
y1<-data1[,2]
x2<-data2[,1]
y2<-data2[,2]
x3<-data3[,1]
y3<-data3[,2]

SD1<-data1[,3]
SD2<-data2[,3]
SD3<-data3[,4]

delta<-runif(5)
errbar(x1,y1,y1+SD1, y1-SD1, col="red",pch=19)
lines(x1,y1,col="red", pch=19, lty=3)
errbar(x2,y2,y2+SD2, y2-SD2, col="green",pch=19)
lines(x2,y2,col="green", pch=19, lty=3)
errbar(x3,y3,y3+SD3, y3-SD3, col="blue",pch=19)
lines(x3,y3,col="blue", pch=19, lty=3)

However, with this code I can obtain only the scatter plot for x1, y1, but
not for the other data sets. Could you please let me know how should I
modify the code presented above ?

In other situations, when I try to make a scatter plot of several data sets
without error bars, I usually use points () function. 
However it does not work in the presented case... 

I would be very grateful for your help. 



--
View this message in context: 
http://r.789695.n4.nabble.com/Scatter-plot-with-error-bars-tp4670502.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Scatter plot with error bars

2013-06-28 Thread beginner
Thank you very much for your help !



--
View this message in context: 
http://r.789695.n4.nabble.com/Scatter-plot-with-error-bars-tp4670502p4670530.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] RStudio blank upon opening

2017-11-17 Thread Beginner via R-help

I'm having a problem: RStudio (on   desktop comp) blank upon opening (after I 
update Win7). I tried different things (reinstalled R and RStudio, backuping  
RStudio settings folder... etc)! C an I launch Rstudio direct from
RGui(32bit)? or some else way to solve this problem? Thanks! P.S. I  launch 
RStudio with Ctrl-RStudio (that is set the path to R)

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] How (in general) take out data sets (available in the packages)?

2017-07-31 Thread Ted Beginner via R-help

Suzen, thank you very much for your so useful information (I will try to 
understand it)!
And my sincere gratitude to the moderator!
>"Suzen, Mehmet" < msu...@gmail.com >:
>I also suggest you Hadley's optimized package for interoperating xls
>files with R:
>https://github.com/tidyverse/readxl
>https://cran.r-project.org/web/packages/readxl/index.html





[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.