【问题标题】:Missing value where TRUE FALSE value needed R ... not a duplicate! Aliens vs. Vampires Simulation需要 TRUE FALSE 值的缺失值 R ...不是重复的!外星人大战吸血鬼模拟
【发布时间】:2016-05-27 01:31:06
【问题描述】:

从这里开始 R 程序员。我正在模拟外星人与吸血鬼以及他们对人类的统治。

此模拟的两个规定:

外星人可以绑架吸血鬼。该数量与吸血鬼目前占世界总人口(包括人类)的百分比成正比。

吸血鬼有健康要求。它们不仅可以将人类变成吸血鬼,而且还需要一些食物和杀戮......

我无法理解这部分代码以及它为什么不起作用:

if (A[i-1] + A.prime * delta.t <= 0){
    A[i]<-0
  } else {
    A[i] <- A[i-1] + A.prime * delta.t 
  }

返回此错误:

if (A[i - 1] + A.prime * delta.t

这是整个代码,我确定这是一个简单的代码错误,而不是逻辑错误。任何帮助将不胜感激!

# Aliens v. Vampires -- version 2.1
# (logistic growth with human reproduction)
# CPSC 420 -- spring 2016


# Set up time.
delta.t <- 1   # years
time <- seq(1940,3000,delta.t)

# Utility functions to convert between i and t.
itot <- function(i) (i-1)*delta.t + 1940
ttoi <- function(t) (t-1940)/delta.t + 1

# Simulation parameters.
init.human.pop <- 7e9
alien.abduction.rate <- 30000      # (beings/year)/year
bite.rate <- .1                # (people/year)/vampire
birth.rate <- .01              # (people/year)/person
sacrifice.rate<- .1 #people/year/vampire
#There is no specific rate for vampires being abducted per year because it is a portion of the alien abduction rate
A <- vector()
V <- vector()
H <- vector()
earth.population <- vector()
V.abductions<-vector()
sacrifice<- vector()

# Initial conditions. (No aliens until 1940, and only one lonely vampire.)
A[1] <- 0
V[1] <- 1
H[1] <- init.human.pop
earth.population[1] <- V[1] + H[1]
V.abductions[1]<-0
sacrifice[1]<-0


# Simulate.
for (i in 2:length(time)) {

  logistic.factor <- H[i-1]/earth.population[i-1]

  # Compute flows.
  VA.prime<-alien.abduction.rate * (time[i]-1940) * (V[i-1]/earth.population[i-1]) #vampires abducted/year
  sacrifice.prime<- sacrifice.rate * (V[i-1]) #sacrifice/year
  V.prime <- bite.rate * V[i-1] * logistic.factor - VA.prime# people/year
  A.prime <- alien.abduction.rate * (time[i] - 1940) * (logistic.factor) + VA.prime   # beings/year
  H.prime <- H[i-1] * birth.rate - (V.prime + (A.prime - VA.prime) + sacrifice.prime)    # people/year
  earth.population.prime <- -A.prime - sacrifice.prime    # people/year

  if(H[i-1] - sacrifice[i-1]<0){
    V.prime <- V.prime - (abs(sacrifice[i-1] - H[i-1])*sacrifice.rate)

  } else {
    V.prime <- bite.rate * V[i-1] * logistic.factor - VA.prime# people/year
  }


  # Compute stocks.
  if (A[i-1] + A.prime * delta.t <= 0){
    A[i]<-0
  } else {
    A[i] <- A[i-1] + A.prime * delta.t 
  }

  # people abducted

  if( V[i-1] + V.prime * delta.t <= 0 ) {
    V[i] <-0
  } else {
    V[i] <- V[i-1] + V.prime * delta.t 
  }               # vampires

  if(H[i-1] + H.prime * delta.t <= 0 ){
    H[i]<-0
  } else {
    H[i]<-H[i-1] + H.prime * delta.t
  } #humans left

  if(V[i] + H[i] <= 0 ){
    earth.population[i]<-0
    }else{
      earth.population[i] <- V[i] + H[i]
    } 

  # people

  V.abductions[i]<- V.abductions[i-1] + VA.prime * delta.t #total vampires abducted thus far
  sacrifice[i]<-sacrifice[i-1] + sacrifice.prime * delta.t #total victims sacrificed to vampires thus far 
}


# Plot results.
all.values <- c(A,V,H,earth.population)
plot(time,A,type="l",col="green",lwd=2,
     ylim=c(min(all.values),max(all.values)),
     main="Aliens v. Vampires apocalypse -- oh my!!",
     xlab="year",
     ylab="# of victims")
lines(time,V,col="red",lwd=2)
lines(time,H,col="black",lwd=1)
lines(time,earth.population,col="brown",lty="dotted",lwd=3)
lines(time,V.abductions,col="purple",lty="dashed",lwd=1)
lines(time,sacrifice,col="blue", lty = 3, lwd=1)
legend("topleft",legend=c("Alien abductions","Vampire bites","Humans","Earthlings", "Vampires Abducted","Human Victims"),
       fill=c("green","red","black","brown", "purple","blue"))

【问题讨论】:

    标签: r if-statement simulation


    【解决方案1】:

    语句A[i-1] + A.prime * delta.t &lt;= 0 的计算结果为NA,这会引发错误。 if 想知道是TRUE 还是FALSE,而NA 两者都不是。

    A.primeNaN,所以这就是您的问题开始的地方。 A.prime 测试A 是否为素数,但由于它是向量,因此该语句不会产生任何值。

    【讨论】:

    • 我不明白您所说的“A 素数测试 A 是否为素数”是什么意思,它应该计算一个值并将其分配给向量 A.prime
    • 啊,是的,我错过了您代码中的那部分。那么这部分不起作用,因为在赋值之后A.prime 等于NaN。您应该尝试调试此作业。先尝试表达式的某些部分,然后找出哪些部分没有按照您期望的方式工作。
    【解决方案2】:

    在某个时刻(步骤 i=336),earth.population 已降至 0,因此 logistic.factor 被 0 除。也许你想添加一个条件进入循环。

    【讨论】:

      猜你喜欢
      • 2015-01-17
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 2015-06-28
      • 1970-01-01
      • 2019-03-07
      • 2013-12-10
      相关资源
      最近更新 更多