user15269
user15269

Reputation: 195

Simulating T-test and linear regression in R

I have two tasks that I have to do in program R. Any hints would be helpful as I am a beginner.

1.Simulate the conditions of T-test and show that, with the conditions of the T-test satisfied, the distribution of the p-values gained by testing corresponds the uniform distribution.

  1. Simulate the conditions of linear regression and show that the evaluators for multidimensional linear regression (three or more parameters) are unbiased. Try to make biased evaluators for the parameters of linear regression and show by simulations that you managed to achieve biasness.

Edit: This is what I tried:

N=1000
res = matrix(rep(0,2*N)), ncol =2)
B0=5
B1=7

for (i in 1:N)
     {
           e=rnorm(100,0,1)
           X=rnorm(100)
           Y=B0 +B1*X +e
           tmp = summary(lm(Y~X))

           k=coef(tmp)
           res[i,1]=k[1]
           res[i,2]=k[2]

     }
 mean(res[,1])
 mean(res[,2])

 mean(res[,1]+3)
 mean(res[,2]+3)

But this is for 2 parameters, I don't know how to do it for 3.

Upvotes: 0

Views: 376

Answers (1)

Bill O'Brien
Bill O'Brien

Reputation: 882

In the code below, the function pvals returns the p-value of a T-test on two randomly generated vectors. We then create a vector of 100 p-values and run the ks.test function to test whether the values are uniformly distributed.

pvals <- function(x){
    set.seed(x)
    df <- data.frame(measure1=runif(100),
                     measure2=runif(100))
    t.test(df[[1]], df[[2]])$p.value}

ks.test(sapply(1:100, function(x) pvals(x)), "punif", -1, 1)


    One-sample Kolmogorov-Smirnov test

data:  sapply(1:100, function(x) pvals(x))
D = 0.50453, p-value < 2.2e-16
alternative hypothesis: two-sided

Upvotes: 2

Related Questions