Is it possible to make ifelse more things at once, as in if?

Viewed 48

I've been trying to sort of write a kernel regression on my own as practice. If I understood the concept correctly, basically I need to sort of divide my data into some segments, and plot the mean of x any y in each segment and draw a line between them. I managed to write a code:

kernel = function(data){
  sor = length(unlist(data[1]))
  for (i in sor) {
    if( data[1]  <  blokk[1]){
      Blokk_1 = subset(data, data[1] < blokk[1])
      x_kor = mean(as.numeric(unlist(Blokk_1[1])))
      y_kor = mean(as.numeric(unlist(Blokk_1[2])))
      points(x_kor, y_kor, pch = 16, cex = 1.5, col = 2)
    } else if ( data[1] >=  blokk[1] & data[1] < blokk[2]){
      Blokk_2 = subset(data, data[1] >= blokk[1] & data[1] < blokk[2])
      x_kor2 = mean(as.numeric(unlist(Blokk_2[1])))
      y_kor2 = mean(as.numeric(unlist(Blokk_2[2])))
      points(x_kor2, y_kor2, pch = 16, cex = 1.5, col = 2)
    } else if (data[1] >= blokk[2] & data[1] < blokk[3]){
      Blokk_3 = subset(data, data[1] >= blokk[2] & data[1] < blokk[3])
      x_kor3 = mean(as.numeric(unlist(Blokk_3[1])))
      y_kor3 = mean(as.numeric(unlist(Blokk_3[2])))
      points(x_kor3, y_kor3, pch = 16, cex = 1.5, col = 2)
    } else if (data[1] > blokk[3]){
      Blokk_4 = subset(data, data[1] > blokk[3])
      x_kor4 = mean(as.numeric(unlist(Blokk_4[1])))
      y_kor4 = mean(as.numeric(unlist(Blokk_4[2])))
      points(x_kor4, y_kor4, pch = 16, cex = 1.5, col = 2)
    }
  }
}

I got a warning message about length (By the way: For some reason in the third block it managed to plot out the point). Googled it, apparently can't use vectors in if function. In another post I saw ifelse as suggestion, tried it aswell.

ifelse(data[1]  <  blokk[1],
       Blokk_1 = subset(data, data[1] < 19)
       x_kor = mean(as.numeric(unlist(Blokk_1[1])))
       y_kor = mean(as.numeric(unlist(Blokk_1[2])))
       points(x_kor, y_kor, pch = 16, cex = 1.5, col = 2),
       ifelse(data[1] >=  blokk[1] & data[1] < blokk[2],
         Blokk_2 = subset(data, data[1] >= blokk[1] & data[1] < blokk[2])
         x_kor2 = mean(as.numeric(unlist(Blokk_2[1])))
         y_kor2 = mean(as.numeric(unlist(Blokk_2[2])))
         points(x_kor2, y_kor2, pch = 16, cex = 1.5, col = 2),
         ifelse(data[1] >= blokk[2] & data[1] < blokk[3],
           Blokk_3 = subset(data, data[1] >= blokk[2] & data[1] < blokk[3])
           x_kor3 = mean(as.numeric(unlist(Blokk_3[1])))
           y_kor3 = mean(as.numeric(unlist(Blokk_3[2])))
           points(x_kor3, y_kor3, pch = 16, cex = 1.5, col = 2),
           ifelse(data[1] > blokk[3])
             Blokk_4 = subset(data, data[1] > blokk[3]),
             x_kor4 = mean(as.numeric(unlist(Blokk_4[1])))
             y_kor4 = mean(as.numeric(unlist(Blokk_4[2])))
             points(x_kor4, y_kor4, pch = 16, cex = 1.5, col = 2)))))

It is not in a function yet, the nested ifelse I wrote, does not work, but when wrote separately, I worked. I don't know, if I made some error, when nesting it, or if I can't put this many output into one 'TRUE' section of ifelse. Thank you in advance if you made it this far.

Edit: Since you asked for the reproducibility, here is the whole data (Note: it was a task within an exam, for linear regression)

 conc = c(19.2, 19.37, 19.23, 19.21, 19.58, 18.94, 19.46, 18.96, 19.26, 18.96, 19.02, 18.90, 19.14, 19.54, 18.96, 18.84, 18.82, 19.45,
 19.59, 19.54, 18.93, 19.51, 19.31, 19.30, 19.05, 18.84, 19.44, 18.82, 19.33, 19.37, 19.00, 19.32, 19.00, 19.27)
effect = c(67.97, 67.72, 67.64, 67.99, 68.32, 67.12, 68.70, 66.92, 67.55, 66.89, 67.00, 66.87, 67.46, 68.45, 66.92, 66.80, 66.96, 67.81,
68.92, 68.57, 67.43, 68.42, 67.87, 67.87, 67.09, 66.83, 68.50, 66.85, 67.78, 67.69, 66.95, 67.49, 67.52, 68.19)

df = data.frame(conc=conc, effect=effect)

blokk = seq(19, 19.4 , 0.2)

Edit 2.0: You were right, the problem is definitely with the 'for', running the if-s separately did the job. Still trying to find out how to solve the 'for'.

0 Answers
Related