R is a function that changes the value of 1 row, one at a time

I am new to R and working on a project, and I was asked to do something more complex than what I can do now.

So, I have a dataset that looks like this:

    Obs.   Democracy  
     1        1 
     2        0
     3        1
     4        0
     5        1
     6        1 
     7        1
     8        0
     9        0
     10       1

The dataset has other control variables, but an important variable is binary for democracy. We performed OLS regression of the current data, and now we collect a list of regressions - where the independent variable changes.

I am trying to create a function in which it changes the democracy variable from 0 to 1 line by line and starts and saves the OLS regression between each change.

I read a little about loops, bundles, and functions, but couldn't write anything close to what we are looking for.

The best I managed to create is not close:

    my.function <- function (data$democracy){
      GB1 <- ifelse(data$democracy[x,]==1, 1, 1)
      return(regression[x] <- glm(leader ~ democracy))
    }

, , , .

!

+3
2

, . , P- . "Super Bowl Theory" .

, , "", , , .

P- OLS R ^ 2 iris:

data(iris)

get_statistic <- function() {
    return(summary(lm(Petal.Length ~ Species, data = iris))$r.squared)
}

## allocate an array to store our results
results <- rep(FALSE, 1000) 

## get the original statistic before we mix things up
original_statistic = get_statistic() 

for (i in 1:1000) { ## do 1000 permutations
    iris$Species = sample(iris$Species, 
        length(iris$Species), 
        replace = F)
    if (get_statistic() >= original_statistic) {
        ## if we beat the original statistic, mark this trial as True
        results[i] = T 
    }
}

## get the average number of trials that succeeded
## this is your p-value!
P_perm = mean(results) 
+3

?

df<-read.table(header=T,text="Obs   Democracy  
1        1 
2        0
3        1
4        0
5        1
6        1 
7        1
8        0
9        0
10       1")

leader<-sample(c("A","B"),10,T)

cases<-expand.grid(lapply(1:nrow(df),function(x)0:1))   # gives you each possible combination of 0:1 * 10

apply(cases,1,function(democracy)glm(democracy~leader)) # returns a list of glms all 1024 combns

PS: 0s 1, cases

zeros<-df$Obs[df$Democracy==0]
cases<-t(sapply(0:length(zeros),function(x){df$Democracy[df$Obs %in% zeros[0:x]]<-1
                                   df$Democracy}))
+3

All Articles