曲线函数不能绘制函数吗?

时间:2018-11-29 23:05:45

标签: r

我看不到为什么函数曲线未显示在我的代码中。

数据:

 EVAnnualWorldSales<-structure(list(Country = structure(c(21L, 21L, 21L, 21L, 21L, 
 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L), .Label = c("Australia", 
 "Brazil", "Canada", "Chile", "China", "Finland", "France", "Germany", 
 "India", "Japan", "Korea", "Mexico", "Netherlands", "New Zealand", 
 "Norway", "Others", "Portugal", "South Africa", "Sweden", "Thailand", 
 "Total", "United Kingdom", "United States"), class = "factor"), 
     Year = c(2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 
     2013, 2014, 2015, 2016, 2017), TotalSales = c(1.89, 2.23, 
     2.69, 5.15, 7.48, 14.26, 61.33, 179.03, 381.3, 703.65, 1239.45, 
     1982.04, 3109.05)), row.names = c(NA, -13L), .Names = c("Country", 
 "Year", "TotalSales"), class = "data.frame")

代码(这里的cuve函数确实画线):

EVAnnualWorldSales<-EV_temp
EV.ss <- nls(TotalSales ~ SSlogis(Year, phi1, phi2, phi3), data = EVAnnualWorldSales)
summary(EV.ss)
alpha <- coef(EV.ss)  #extracting coefficients
plot(TotalSales ~ Year, data = EVAnnualWorldSales, main = "Logistic Growth Model of EV Sales", 
 xlab = "Year", ylab = "Sales", xlim = c(2004, 2050), ylim = c(0, 11000))  # 
curve(alpha[1]/(1 + exp(-(x - alpha[2])/alpha[3])), add = T, col = "blue")  # Fitted model

enter image description here

B代码(此处的cuve函数未画线):

y <- EVAnnualWorldSales$TotalSales/11000
x_yrs <- EVAnnualWorldSales$Year-2004
    my_model <- nls(y ~ 1/(1+exp(a*x_yrs +b)),start=list(a=-0.5,b=-0.5))
plot(EVAnnualWorldSales$Year,y*11000)
Years<-seq(2005, 2050, 1)-2004
alpha <- coef(my_model)
curve((1/(1+exp(alpha[1]*x +alpha[2])))*11000, add = T, col = "blue") 

enter image description here

为什么曲线在代码块A中而不在代码块B中起作用?

1 个答案:

答案 0 :(得分:0)

抱歉,看不到它是一个简单的解决方案。

将代码更改为:plot(x_yrs,y * 11000)

相关问题