title: Error in nrow(X) : object 'X' not found , but it gets defined
body: <p>I am trying to implement logistic regression and the function works manually, but for some reason I get the error "Error in nrow(X) : object 'X' not found ", even though X is defined before the nrow command. I use the UCI Data "Adult" to test it.</p>
  
  <p>If i try to run the function manually there is no error. Can anyone explain that?</p>
  
  <pre><code># Sigmoidfunction
  sigmoid &lt;- function(z){
    g &lt;- 1/(1+exp(-z))
    return(g)
  }
  
  # Costfunction
  cost &lt;- function(theta){
    n &lt;- nrow(X)
    g &lt;- sigmoid(X %*% theta)
    J &lt;- (1/n)*sum((-Y*log(g)) - ((1-Y)*log(1-g)))
    return(J)
  }
  
  log_reg &lt;- function(datafr, m){
  
    # Train- und Testdaten Split
    sample &lt;- sample(1:nrow(datafr), m)
    df_train &lt;- datafr[sample,]
    df_test &lt;- datafr[-sample,]
  
    num_features &lt;- ncol(datafr) - 1
    num_label &lt;- ncol(datafr)
    label_levels &lt;- levels(datafr[, num_label])
    datafr[, num_features+1] &lt;- ifelse(datafr[, num_label] == names(table(datafr[,num_label]))[1], 0, 1)
  
    # Predictor variables
    X &lt;- as.matrix(df_train[, 1:num_features])
    X_test &lt;- as.matrix(df_test[, 1:num_features])
  
    # Add ones to X
    X &lt;- cbind(rep(1, nrow(X)), X)
    X_test &lt;- cbind(rep(1, nrow(X_test)), X_test)
  
    # Response variable
    Y &lt;- as.matrix(df_train[, num_label] )
    Y &lt;- ifelse(Y == names(table(Y))[1], 0, 1)
  
    Y_test &lt;- as.matrix(df_test[, num_label] )
    Y_test &lt;- ifelse(Y_test == names(table(Y_test))[1], 0, 1)
  
  
    # Intial theta
    initial_theta &lt;- rep(0, ncol(X))
  
    # Derive theta using gradient descent using optim function
    theta_optim &lt;- optim(par=initial_theta, fn=cost)
  
    predictions &lt;- ifelse(sigmoid(X_test%*%theta_optim$par)&gt;=0.5, 1, 0)
  
  
  # Generalization error
  error_rate &lt;- sum(predictions!=Y_test)/length(Y_test)
  
  return(error_rate)
  }
  
  ### Adult Data
  data &lt;- read.table('https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data', 
                      sep = ',', fill = F, strip.white = T)
  colnames(data) &lt;- c('age', 'workclass', 'fnlwgt', 'education', 
                      'education_num', 'marital_status', 'occupation', 'relationship', 'race', 'sex', 
                      'capital_gain', 'capital_loss', 'hours_per_week', 'native_country', 'income')
  
  # Featureselection
  datafr &lt;- data[, c("age", "education_num", "hours_per_week", "income")]
  
  log_reg(datafr = datafr, m = 20)
  </code></pre>