Re: [R] a repetition of simulation
Hello, In addition to my question a few days ago, Now I have a matrix of the coefficients, how can I see all the P.Values (Pr(|z|)) of the covariates from the 1000 iterations? I tried names(log_v) and couldn'n find it. Thank you, Sigalit. On 11/13/07, Julian Burgos [EMAIL PROTECTED] wrote: Well, the obvious (but perhaps not the most elegant) solution is put everything in a loop and run it 600 times. coefficients=matrix(NA,ncol=3,nrow=600) for (loop in 1:600){ [all your code here] coefficients[loop,]=coef(log_v) } That will give you a matrix with the coefficients of each model run in each row. Julian sigalit mangut-leiba wrote: I want to repeat the simulation 600 times and to get a vector of 600 coefficients for every covariate: aps and tiss. Sigalit. On 11/13/07, Julian Burgos [EMAIL PROTECTED] wrote: And what is your question? Julian sigalit mangut-leiba wrote: Hello, I have a simple (?) simulation problem. I'm doing a simulation with logistic model and I want to reapet it 600 times. The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) I want to do a repetition of this 600 times (I want to have 600 logistic models), and see all the coefficients of the covariates aps tiss. Thanks in advance, Sigalit. [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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] a repetition of simulation
Excuse me, but I think your code deserves some comments. Unfortunately, the history of postings is in reverse order, so I'll address your first question first: The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] If I'm getting this correctly, you don't need z and x later on? Then y - rbinom(500, 1, .6*.95) should do the trick. if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) tiss - ifelse(y, rnorm(500, mean=20.731, sd=9.751), rnorm(500, mean=18.531, sd=9.499)) Likewise for aps. } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) Makes me wonder what you need aps and tiss for. Let's assume for a moment that they are the coefficients. I do not see the necessity to put everything into a data frame, so log_v - glm(y ~ aps+tiss, family=binomial) should be sufficient and summary(log_v)$coefficients[,Pr(|z|)] extracts the p value. how can I see all the P.Values (Pr(|z|)) of the covariates from the 1000 iterations? I tried names(log_v) and couldn'n find it. Try str(summary(log_v)) and you see the whole structure. -- Johannes H�sing There is something fascinating about science. One gets such wholesale returns of conjecture mailto:[EMAIL PROTECTED] from such a trifling investment of fact. http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi) __ 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] a repetition of simulation
summary(log_v) Julian sigalit mangut-leiba wrote: Hello, In addition to my question a few days ago, Now I have a matrix of the coefficients, how can I see all the P.Values (Pr(|z|)) of the covariates from the 1000 iterations? I tried names(log_v) and couldn'n find it. Thank you, Sigalit. On 11/13/07, Julian Burgos [EMAIL PROTECTED] wrote: Well, the obvious (but perhaps not the most elegant) solution is put everything in a loop and run it 600 times. coefficients=matrix(NA,ncol=3,nrow=600) for (loop in 1:600){ [all your code here] coefficients[loop,]=coef(log_v) } That will give you a matrix with the coefficients of each model run in each row. Julian sigalit mangut-leiba wrote: I want to repeat the simulation 600 times and to get a vector of 600 coefficients for every covariate: aps and tiss. Sigalit. On 11/13/07, Julian Burgos [EMAIL PROTECTED] wrote: And what is your question? Julian sigalit mangut-leiba wrote: Hello, I have a simple (?) simulation problem. I'm doing a simulation with logistic model and I want to reapet it 600 times. The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) I want to do a repetition of this 600 times (I want to have 600 logistic models), and see all the coefficients of the covariates aps tiss. Thanks in advance, Sigalit. [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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-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] a repetition of simulation
Thank you for all your comments, Sigalit. On 11/15/07, Johannes Hüsing [EMAIL PROTECTED] wrote: Excuse me, but I think your code deserves some comments. Unfortunately, the history of postings is in reverse order, so I'll address your first question first: The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] If I'm getting this correctly, you don't need z and x later on? Then y - rbinom(500, 1, .6*.95) should do the trick. if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) tiss - ifelse(y, rnorm(500, mean=20.731, sd=9.751), rnorm(500, mean= 18.531, sd=9.499)) Likewise for aps. } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) Makes me wonder what you need aps and tiss for. Let's assume for a moment that they are the coefficients. I do not see the necessity to put everything into a data frame, so log_v - glm(y ~ aps+tiss, family=binomial) should be sufficient and summary(log_v)$coefficients[,Pr(|z|)] extracts the p value. how can I see all the P.Values (Pr(|z|)) of the covariates from the 1000 iterations? I tried names(log_v) and couldn'n find it. Try str(summary(log_v)) and you see the whole structure. -- Johannes Hüsing There is something fascinating about science. One gets such wholesale returns of conjecture mailto:[EMAIL PROTECTED] from such a trifling investment of fact. http://derwisch.wikidot.com (Mark Twain, Life on the Mississippi) [[alternative HTML version deleted]] __ 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] a repetition of simulation
From: sigalit mangut-leiba [EMAIL PROTECTED] Date: Nov 15, 2007 3:24 PM Subject: Re: [R] a repetition of simulation To: r-help [EMAIL PROTECTED] Hello, In addition to my question a few days ago, Now I have a matrix of the coefficients, how can I see all the P.Values ( Pr(|z|)) of the covariates from the 1000 iterations? I tried names(log_v) and couldn'n find it. Thank you, Sigalit. On 11/13/07, Julian Burgos [EMAIL PROTECTED] wrote: Well, the obvious (but perhaps not the most elegant) solution is put everything in a loop and run it 600 times. coefficients=matrix(NA,ncol=3,nrow=600) for (loop in 1:600){ [all your code here] coefficients[loop,]=coef(log_v) } That will give you a matrix with the coefficients of each model run in each row. Julian sigalit mangut-leiba wrote: I want to repeat the simulation 600 times and to get a vector of 600 coefficients for every covariate: aps and tiss. Sigalit. On 11/13/07, Julian Burgos [EMAIL PROTECTED] wrote: And what is your question? Julian sigalit mangut-leiba wrote: Hello, I have a simple (?) simulation problem. I'm doing a simulation with logistic model and I want to reapet it 600 times. The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean= 20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) I want to do a repetition of this 600 times (I want to have 600 logistic models), and see all the coefficients of the covariates aps tiss. Thanks in advance, Sigalit. [[alternative HTML version deleted]] __ 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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ 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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ 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] a repetition of simulation
I want to repeat the simulation 600 times and to get a vector of 600 coefficients for every covariate: aps and tiss. Sigalit. On 11/13/07, Julian Burgos [EMAIL PROTECTED] wrote: And what is your question? Julian sigalit mangut-leiba wrote: Hello, I have a simple (?) simulation problem. I'm doing a simulation with logistic model and I want to reapet it 600 times. The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) I want to do a repetition of this 600 times (I want to have 600 logistic models), and see all the coefficients of the covariates aps tiss. Thanks in advance, Sigalit. [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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] a repetition of simulation
Thank you, I changed that and it's much more efficient. Sigalit. On 11/13/07, Phil Spector [EMAIL PROTECTED] wrote: You can use the replicate function to do your simulation. First, put the code to do one repetition in a function: dosim0 = function(n=500){ x - 0 y - 0 z - 0 aps - 0 tiss - 0 for (i in 1:n){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) } v - data.frame(y, aps, tiss) glm(y~., family=binomial, data=v)$coef } Now you can run results = t(replicate(1000,dosim0())) to get a 1000x3 matrix with the coefficients from each simulation. Before you actually do the simulations, you might want to rewrite your function so that it's not so inefficient. Functions in R are vectorized, and you should take advantage of that whenever possible. Here's a vectorized version of your simulation function: dosim = function(n=500){ z = rbinom(n,1,.6) x = rbinom(n,1,.95) y = z * x aps = ifelse(y == 1,rnorm(n,mean=13.4,sd=7.09),rnorm(n,mean=12.67, sd= 6.82)) tiss = ifelse(y == 1,rnorm(n,mean=20.731,sd=9.751),rnorm(n,mean=18.531 ,sd=9.499)) glm(y~.,family=binomial,data=data.frame(y,aps,tiss))$coef } Now, you can run results = t(replicate(1000,dosim())) to get a similar result. Notice the difference in time for the two functions: system.time(results - t(replicate(1000,dosim0( user system elapsed 55.123 0.004 55.239 system.time(results - t(replicate(1000,dosim( user system elapsed 20.297 0.000 20.295 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley [EMAIL PROTECTED] [[alternative HTML version deleted]] __ 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] a repetition of simulation
Well, the obvious (but perhaps not the most elegant) solution is put everything in a loop and run it 600 times. coefficients=matrix(NA,ncol=3,nrow=600) for (loop in 1:600){ [all your code here] coefficients[loop,]=coef(log_v) } That will give you a matrix with the coefficients of each model run in each row. Julian sigalit mangut-leiba wrote: I want to repeat the simulation 600 times and to get a vector of 600 coefficients for every covariate: aps and tiss. Sigalit. On 11/13/07, Julian Burgos [EMAIL PROTECTED] wrote: And what is your question? Julian sigalit mangut-leiba wrote: Hello, I have a simple (?) simulation problem. I'm doing a simulation with logistic model and I want to reapet it 600 times. The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) I want to do a repetition of this 600 times (I want to have 600 logistic models), and see all the coefficients of the covariates aps tiss. Thanks in advance, Sigalit. [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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-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] a repetition of simulation
Hello, I have a simple (?) simulation problem. I'm doing a simulation with logistic model and I want to reapet it 600 times. The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) I want to do a repetition of this 600 times (I want to have 600 logistic models), and see all the coefficients of the covariates aps tiss. Thanks in advance, Sigalit. [[alternative HTML version deleted]] __ 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] a repetition of simulation
Dear Sigalit, On Nov 12, 2007 2:18 PM, sigalit mangut-leiba [EMAIL PROTECTED] wrote: Hello, I have a simple (?) simulation problem. I'm doing a simulation with logistic model and I want to reapet it 600 times. The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) I want to do a repetition of this 600 times (I want to have 600 logistic models), and see all the coefficients of the covariates aps tiss. Thanks in advance, Sigalit. I won't comment on your general approach to simulation but does this answer your question? ### Create an empty matrix to hold the results as they ### are created Results - matrix(NA, ncol = 3, nrow = 600) for(i in 1:600) { generate your data Results[i, ] - coef(glm(y~., family=binomial, data=v)) } summary(Results) Stephen -- Rochester, Minn. USA __ 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] a repetition of simulation
Thank you, Sigalit. On 11/13/07, Moshe Olshansky [EMAIL PROTECTED] wrote: Hi, Look at names(log_v) in your notation to see what you really need. Then you could do something like: results - list(600) for (ij in 1:600) { do what you did results[[ij]] - log_v } So now you have a list of 600 results and can do anything you need (look at lapply/sapply). Regards, Moshe. --- sigalit mangut-leiba [EMAIL PROTECTED] wrote: Hello, I have a simple (?) simulation problem. I'm doing a simulation with logistic model and I want to reapet it 600 times. The simulation looks like this: z - 0 x - 0 y - 0 aps - 0 tiss - 0 for (i in 1:500){ z[i] - rbinom(1, 1, .6) x[i] - rbinom(1, 1, .95) y[i] - z[i]*x[i] if (y[i]==1) aps[i] - rnorm(1,mean=13.4, sd=7.09) else aps[i] - rnorm(1,mean=12.67, sd=6.82) if (y[i]==1) tiss[i] - rnorm(1,mean=20.731,sd=9.751) else tiss[i] - rnorm(1,mean=18.531,sd=9.499) } v - data.frame(y, aps, tiss) log_v - glm(y~., family=binomial, data=v) summary(log_v) I want to do a repetition of this 600 times (I want to have 600 logistic models), and see all the coefficients of the covariates aps tiss. Thanks in advance, Sigalit. [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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.