1

Objective: The overall objective of the problem is to calculate the confidence interval (CI) of various sample sizes (n=2,4..1024) of rnorm, 10,000 times and then count the number of times each one fails (this likely requires a counter and an if/else statement). Finally the results are to be plotted

I am trying to calculate CI of the means for several simulations of a sample sizes, however, I am first trying to break down the code for one specific sample size a = 8.

The problem I have is that I do not know how to generate a linear model for each row. Would anyone know how I can do this? Here is what I have so far:

a <- 8
n.sim.3 <- 10000

for ( i in a) {
  r.mat <- matrix(rnorm(i*n.sim.3), nrow=n.sim.3, ncol = a)
  lm.tmp <- apply(three.mat,1,lm(n.sim.3~1)  # The lm command is where I'm stuck I don't think this is correct)
  confint.tmp <- confint(lm.tmp)
exracon
  • 23
  • 4
  • Hi exracon! Please add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! I think you are right and your call to `lapply` does not look right. I'd start with `?lapply` and `?lm` and if you provide a MRE i also will give it a try. – dario Feb 09 '20 at 17:25
  • Hi dario - I am having a hard time trying to break this problem down for the rows. I can figure out how to calculate linear models for 1 sample size in 1 simulation. I don't know how to scale it. – exracon Feb 09 '20 at 17:43
  • Are you trying to model one row or a bunch of rows together that you create randomly? Also if you are just trying to find the CI of mean of random samples then you don't need to fit a model. You calculate `z score` and the CI will be sample average `+/- zscore` – XXavier Feb 09 '20 at 17:45
  • @exracon So far it's not clear to me what your question is (other that your code is not running). And without MRE it's impossible for others to replicate your problem. I'd suggest reading [this link on MRE](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and rephrasing your question in a clearer way and with **minimal** reproducible example code. I know, that is not always easy, but without that I can't help you much. Best of luck ;) – dario Feb 09 '20 at 17:53
  • 1
    Here is an example of applying lm to rows of an x matrix. Hope this might be helpful `x <- 1:10` `mat <- matrix(rnorm(200), nrow=20, ncol=10)` `resultcoeffs <- apply(mat, 1, function(y) lm(y ~ x)$coefficients)` – XXavier Feb 09 '20 at 18:17
  • Hi XXavier -- I think this might be what I need. I wanted to understand how to apply a linear model over several rows. The reason that I need the linear model is to make a confint() of the 1000 models to evalute how many of them DO NOT contain the true underlying mean of rnorm which is 0. – exracon Feb 09 '20 at 18:57

0 Answers0