AYY
AYY

Reputation: 59

R plot function gives out weird answer

I tried to use R plot to get the curve for some functions. Sometimes, I got very weird results. Here is an example

u=c(2,2,2,2,2)
elas=function(P){
  prob=P/sum(u,P)
  return(prob)
}
plot(elas,0,6)

This code gives out the plot like this: enter image description here

It is obviously not right. The right code should be like this: enter image description here

I know that if I change the 3rd line of the code to be

  prob=P/(sum(u)+P)

It would work. But I do not understand why my original code does not work. Does it mean that I cannot plot a function which embeds another function?

Upvotes: 1

Views: 148

Answers (2)

Dave Rosenman
Dave Rosenman

Reputation: 1467

sum(u+P) = sum of each value in u plus P

sum(u) + P = sum of values in u plus P.

Example: u = c(1,2,3), P = 5

sum(u+P) = (1+5) + (2+5) + (3+5) = 6+7+8 = 21
sum(u) + P = (1+2+3) + 5 = 6 + 5 = 11

For

elas <- function(P){
    prob=P/(sum(u+P)
    return(prob)
}

with u <- c(2,2,2,2,2):

y <- elas(0:6)
print(y)
#output of print y:
0.00000000 0.03225806 0.06451613 0.09677419 0.12903226 0.16129032
0.19354839
plot(0:6,y)

enter image description here

For

elas <- function(P){
    prob=P/(sum(u) + P)
    return(prob)
}
y <- elas(0:6)
print(y)
#Output of print(y)
plot(0:6,y)
0.00000000 0.09090909 0.16666667 0.23076923 0.28571429 0.33333333 0.37500000

enter image description here

Upvotes: 1

eipi10
eipi10

Reputation: 93891

sum(u,P) is a single value equal to the sum of all the values in u and P. So in elas, every value of P get's divided by the same number (313 in your example).

sum(u) + P is a vector containing each individual value of P with sum(u) added to it. So in the second version of elas (which I've called elas2 below), P/(sum(u) + P) results in element-by-element division of P by sum(u) + P.

Consider the examples below.

u=c(2,2,2,2,2)
x=seq(0,6,length=101)

sum(u,x)
[1] 313
sum(u) + x
  [1] 10.00 10.06 10.12 10.18 10.24 10.30 10.36 10.42 10.48 10.54 10.60 10.66 10.72 10.78
 [15] 10.84 10.90 10.96 11.02 11.08 11.14 11.20 11.26 11.32 11.38 11.44 11.50 11.56 11.62
 [29] 11.68 11.74 11.80 11.86 11.92 11.98 12.04 12.10 12.16 12.22 12.28 12.34 12.40 12.46
 [43] 12.52 12.58 12.64 12.70 12.76 12.82 12.88 12.94 13.00 13.06 13.12 13.18 13.24 13.30
 [57] 13.36 13.42 13.48 13.54 13.60 13.66 13.72 13.78 13.84 13.90 13.96 14.02 14.08 14.14
 [71] 14.20 14.26 14.32 14.38 14.44 14.50 14.56 14.62 14.68 14.74 14.80 14.86 14.92 14.98
 [85] 15.04 15.10 15.16 15.22 15.28 15.34 15.40 15.46 15.52 15.58 15.64 15.70 15.76 15.82
 [99] 15.88 15.94 16.00
par(mfrow=c(1,3))

elas=function(P) {
   P/sum(u,P)
}

dat = data.frame(x, y=elas(x), y_calc=x/sum(u,x))

plot(dat$x, dat$y, type="l", lwd=2, ylim=c(0,0.020))
plot(elas, 0, 6, lwd=2, ylim=c(0,0.020))
curve(elas, 0, 6, lwd=2, ylim=c(0,0.020))

enter image description here

dat$y - dat$y_calc
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[43] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[85] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
elas2 = function(P) {
  P/(sum(u) + P)
}

dat$y2 = elas2(x)

plot(dat$x, dat$y2, type="l", lwd=2, ylim=c(0,0.4))
plot(elas2, 0, 6, lwd=2, ylim=c(0,0.4))
curve(elas2, 0, 6, lwd=2, ylim=c(0,0.4))

enter image description here

Upvotes: 1

Related Questions