[R] 循环查找图中的间隔并将它们用于最终公式和绘图
[R]loops for to find the intervals in the graph and use them for the final formula and plot
这是我的代码:
Y<-V40 # variable of my dataset (includes 0 and 1)
X<-V18 # other variable of my dataset
plot(X,Y)
# loops for to find the intervals in the graph and use them for the final formula and plot
for (i in 1: X) {
if(X[i]<40){
AN[i]# Here I would like to find the first interval
AN[i]<- 14 # if x is equal to 40 I would like to first take the interval from 0 to 10 and count the numbers of 0 and 1(dots) that are in the plot
AP[i]<-16 # same speech and i count 1
N[i]<-AN[i] + AP[i] #N
MAX[i]=AP[i] * log(AP[i]/N[i]) + AN[i] *log(AN[i]/N[i]) # formula for entering new data
print(MAX[i])
lines(Y~X,ADD=MAX[i], xlab='CBO', ylab='Fault-Proneness')
}
if(X[i]>40) # otherwise examine the other interval
{
AN[i]<- 1 # if x is equal to 40 I would like to first take the interval from 0 to 10 and count the numbers of 0 and 1(dots) that are in the plot
AP[i]<-3 # same speech and i count 1
N[i]<-AN[i] + AP[i] #tot
MAX[i]=AP[i] * log(AP[i]/N[i]) + AN[i] *log(AN[i]/N[i]) # formula for entering new data #print
print(MAX[i]) #print
lines(Y~X,ADD=MAX[i], xlab='CBO', ylab='Fault-Proneness')
}
}
这是我的情节:Plot
这些是指令:Directives
这些是地块:PlotExample
我想知道是否可以在特定区间内计算此图的0和1的数量,并根据找到的值找到MAX并将其添加到图中。
我认为您不需要循环 .. 从您的第一个代码开始:
x<-c(23,45,65,46,86,54,78,65,4,3,23,43,65,21,34)
y<-c(0,1,1,0,1,0,1,0,1,1,0,1,1,0,0)
plot(x,y)
xPos1 <- sum(y[x<=40]==1)
xNeg1 <- sum(y[x<=40]==0)
xPos2 <- sum(y[x>40]==1)
xNeg2 <- sum(y[x>40]==0)
NumebrTot1 <- xPos1+xNeg1
NumebrTot2 <- xPos2+xNeg2
Max1 <- xPos1 * log(xPos1/NumebrTot1) + xNeg1 *log(xNeg1/NumebrTot1)
Max2 <- xPos2 * log(xPos2/NumebrTot2) + xNeg2 *log(xNeg2/NumebrTot2)
然后,我不知道要绘制什么,因为根据构造,最大值将为负...
这是我的代码:
Y<-V40 # variable of my dataset (includes 0 and 1)
X<-V18 # other variable of my dataset
plot(X,Y)
# loops for to find the intervals in the graph and use them for the final formula and plot
for (i in 1: X) {
if(X[i]<40){
AN[i]# Here I would like to find the first interval
AN[i]<- 14 # if x is equal to 40 I would like to first take the interval from 0 to 10 and count the numbers of 0 and 1(dots) that are in the plot
AP[i]<-16 # same speech and i count 1
N[i]<-AN[i] + AP[i] #N
MAX[i]=AP[i] * log(AP[i]/N[i]) + AN[i] *log(AN[i]/N[i]) # formula for entering new data
print(MAX[i])
lines(Y~X,ADD=MAX[i], xlab='CBO', ylab='Fault-Proneness')
}
if(X[i]>40) # otherwise examine the other interval
{
AN[i]<- 1 # if x is equal to 40 I would like to first take the interval from 0 to 10 and count the numbers of 0 and 1(dots) that are in the plot
AP[i]<-3 # same speech and i count 1
N[i]<-AN[i] + AP[i] #tot
MAX[i]=AP[i] * log(AP[i]/N[i]) + AN[i] *log(AN[i]/N[i]) # formula for entering new data #print
print(MAX[i]) #print
lines(Y~X,ADD=MAX[i], xlab='CBO', ylab='Fault-Proneness')
}
}
这是我的情节:Plot
这些是指令:Directives
这些是地块:PlotExample
我想知道是否可以在特定区间内计算此图的0和1的数量,并根据找到的值找到MAX并将其添加到图中。
我认为您不需要循环 .. 从您的第一个代码开始:
x<-c(23,45,65,46,86,54,78,65,4,3,23,43,65,21,34)
y<-c(0,1,1,0,1,0,1,0,1,1,0,1,1,0,0)
plot(x,y)
xPos1 <- sum(y[x<=40]==1)
xNeg1 <- sum(y[x<=40]==0)
xPos2 <- sum(y[x>40]==1)
xNeg2 <- sum(y[x>40]==0)
NumebrTot1 <- xPos1+xNeg1
NumebrTot2 <- xPos2+xNeg2
Max1 <- xPos1 * log(xPos1/NumebrTot1) + xNeg1 *log(xNeg1/NumebrTot1)
Max2 <- xPos2 * log(xPos2/NumebrTot2) + xNeg2 *log(xNeg2/NumebrTot2)
然后,我不知道要绘制什么,因为根据构造,最大值将为负...