0

I need to do the following: I would like to increase variable pi1 from -1 to 0 by 0.0001 steps under the condition that all the other variables stay the same. As a second step I need to select 1000 samples for each different pi1 value. In the end, I would need to measure the biasedness of both regressions to the real value. After a thorough investigation, I really don't see why I both loops are not working.

This sort of an idea how it could work

index <- seq(1, 1000)
    
beta_OLS <- NULL
beta_IV <- NULL

for(i in seq(from = -1, to = 0, by = 0.001)) {
  for(k in index) {
    n <- 2000
    pi1 <- i
    b0 <- 0
    b1 <- 0
    b2 <- -1/1000
    b3 <- 1/5
    
    z <- runif(n, 0, 25)
    ov <- rnorm(n, 0, 1)
    
    d <- -1/2 + pi1 * z + 1/2 * ov + rnorm(n, 0, 1) > 0
    y <- b0 + b1 * d + b2 * z + b3 * ov + rnorm(n, 0, 1/10)
    
    #OLS Regression
    model12 = lm(y ~ d, data = data)
    beta_OLS[k] = model12$coefficients[2]
      
    #IV Regression
    model12_1 = ivreg(y ~ d | z, data=data)
    beta_IV[k] = model12_1$coefficients[2]
  }
}

real_value <- - 1/1000

average_OLS <- mean(beta_OLS)
average_IV <- mean(beta_IV)

biased_OLS <- average_OLS - real_value 
biased_IV <- average_IV - real_value 

biased_OLS 
biased_IV
slamballais
  • 3,161
  • 3
  • 18
  • 29
freddywit
  • 301
  • 1
  • 5
  • What is the issue you are experiencing? Please provide us with a small, reproducible code snippet that we can copy and paste to better understand the issue and test possible solutions. You can share datasets with `dput(YOUR_DATASET)` or smaller samples with `dput(head(YOUR_DATASET))`. (See [this answer](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610) for detailed instructions.) – ktiu Jun 05 '21 at 16:45

1 Answers1

1

Did a few alterations here and it ran. Not sure if the result are right.

Since both loops had the same count, eliminated one of them.

Also attributed the results of d and y on every run to a dataframe called data (that you used as source for your regressions).

index = seq(1,1000)

beta_OLS = NULL
beta_IV = NULL
i = -1

for(k in index){
     n <- 2000
     pi1 <- i
     b0 <- 0
     b1 <- 0
     b2 <- -1/1000
     b3 <- 1/5
     
     z <- runif(n,0,25)
     ov <- rnorm(n,0,1)
     
     d <- -1/2 + pi1 * z + 1/2 * ov + rnorm(n,0,1) > 0
     y <- b0 + b1 * d + b2 * z + b3 * ov + rnorm(n,0,1/10)
     
     data = as.data.frame(cbind(y,d))
     
     #OLS Regression
     model12 = lm(y ~ d, data = data)
     beta_OLS[k] = model12$coefficients[2]
     
     #IV Regression
     model12_1 = ivreg::ivreg(y ~ d | z, data=data)
     beta_IV[k] = model12_1$coefficients[2]
     
     pi1 <- i + 0.001
}



real_value = - 1/1000

average_OLS = mean(beta_OLS)
average_IV = mean(beta_IV)

biased_OLS = average_OLS - real_value 
biased_IV = average_IV - real_value 

biased_OLS 
biased_IV