2017-02-28 94 views
1

我有一些關於價格和迴歸的需求(數量)方程的數據。下面是重複性數據的dput()當我更換x-lim時,我的拋物線沿着x軸移動

https://gist.github.com/hack-r/28d0f6845eafc7935f6cbb74fdd09637

我試圖讓一個情節,看起來像這一個,這樣我就知道價格最優在於:

enter image description here

但是,我從上面得到的圖的example沒有提供它的數據並且省略了一些代碼,所以我不清楚它們是如何得到它的。

我的數據是這樣的:

head(mydat) 
gross_price  cost quantity net_price 
204  895.000 47.51235  16 847.4877 
159  920.000 66.10084  3 853.8992 
149  950.000 75.67797  6 874.3220 
201  895.000 13.03794  10 881.9621 
217 1016.329 114.95628  9 901.3731 
288 1132.306 90.63706  16 1041.6690 

所以,我這樣做:

par(mar = c(5,5,2,5)) 

with(mydat, plot(net_price, quantity,pch=1,xlab="Price",ylab="Quantity", xlim=c(0,1250),ylim=c(0,800))) 
abline(a=(30.6117202), b=-0.0279182, col="green") # Demand Fn coef.s from regression 

par(new = T) 
mydat$predicted_q <- (30.6117202-0.0279182 * mydat$net_price) 
mydat$profit  <- mydat$predicted_q * mydat$net_price # quantity * net price 

with(mydat, plot(net_price, profit, type="l", col="red3",axes=F, xlab=NA, ylab=NA)) 

這給了我這樣的:

plot - 注意在撰寫本文時,Stack Overflow的圖像主機(imgur.com)發生重大中斷,所以我無法正確顯示圖形,因此鏈接。

但它看起來像紅線(拋物線)有點正確的中心,所以我試着改變xlim看看發生了什麼。顯然拋物線停留在同一個地方,而不管其餘的數據如何。這不好;這意味着我們無法正確理解情節。

par(mar = c(5,5,2,5)) 

with(mydat, plot(net_price, quantity,pch=1,xlab="Price",ylab="Quantity", xlim=c(0,3000),ylim=c(0,800))) 
abline(a=(30.6117202), b=-0.0279182, col="green") # Demand Fn coef.s from regression 

par(new = T) 
mydat$predicted_q <- (30.6117202-0.0279182 * mydat$net_price) 
mydat$profit  <- mydat$predicted_q * mydat$net_price # quantity * net price 

with(mydat, plot(net_price, profit, type="l", col="red3",axes=F, xlab=NA, ylab=NA)) 

http://imageshack.com/a/img923/6971/r1gG6I.png

所以我想對自己說,這個問題必須是拋物線被繪出了從其餘部分不同的層上,也許我可以lines()解決這個問題:

par(mar = c(5,5,2,5)) 

with(mydat, plot(net_price, quantity,pch=1,xlab="Price",ylab="Quantity", xlim=c(0,3000),ylim=c(0,800))) 
abline(a=(30.6117202), b=-0.0279182, col="green") # Demand Fn coef.s from regression 

par(new = T) 
mydat$predicted_q <- (30.6117202-0.0279182 * mydat$net_price) 
mydat$profit  <- mydat$predicted_q * mydat$net_price # quantity * net price 

#with(mydat, plot(net_price, profit, type="l", col="red3",axes=F, xlab=NA, ylab=NA)) 
lines(mydat$profit, col="red3",type="l") 

但是,這個結果甚至不是一個很好的光滑的拋物線,這是我需要這個有用的...

回答

1

添加第一個圖xlim到拋物線圖。

with(mydat, plot(net_price, profit, type="l", col="red3",axes=F, 
       xlab=NA, ylab=NA,xlim=c(0,1250))) 

https://imageshack.com/i/poFMW7asj

而且

with(mydat, plot(net_price, profit, type="l", col="red3",axes=F, 
       xlab=NA, ylab=NA, xlim = c(0,3000))) 

https://imageshack.com/i/pn0VYxikj

+1

是啊,這是有道理的!我要告訴自己,如果不是我以此爲基礎的壞榜樣,我會意識到這一點。 –

+0

如果你設置了'axes = T',你就會意識到拋物線有不同的軸。 –

+0

沒錯。或'axis(side = 4)'。我只是盲目地跟隨這個例子。我不認爲我使用的示例的OP知道這個問題。 –