다른 함수 (이 함수를 모두 정의한) 내에서 함수 (A라고 함)를 사용하려고합니다 (B라고 함). 이를 위해 B 함수의 시작 부분에서 A의 인수 중 일부를 정의한 다음 do.call을 사용하여 A를 호출했습니다. 그러나 함수 A는 방금 정의 되었더라도 인수를 인식하지 못합니다. 무슨 일이야?누락 된 인수를 반환하는 Do.call 사용자 정의 함수 R
컨텍스트 : 간단한 시스템의 시뮬레이션을 실행하려고합니다. 이를 위해 여러 가지 하위 기능으로 문제를 해결합니다. 하나는 (genpar) 시뮬레이션에 사용할 임의의 매개 변수를 생성하고 anther (indeffbasic)는 매개 변수 값을 가변 값으로 매핑합니다. 그런 다음이 두 함수를 indeff라는 새로운 함수로 결합합니다. 이 마지막 단계에서 오류가 발생합니다. 나는 주위를 수색했지만 어떤 해결책도 찾을 수 없다. 대답이 분명하다면 용서해주십시오. 아래 코드 참조 : 라인에서
#First we set up the data structure.
#In this simple model we have N variables, and each variable has a value at each time period.
#There are T time periods.
N <- 2
T <- 20
variables <- data.frame(matrix(0,T,N))
#I assign names to the variables, and check that I've given the right number of names
Names <- c("Movement", "AIpapers")
if(length(Names)==N){names(variables) <- Names} else {print("Error")}
#Now I assign the basic function that, given parameter values, runs a simulation over T time periods.
indeffbasic <- function(a0=5000, b0=100, a1, b1){
for (i in 1:T) {
variables[i, "Movement"] <- (if(i-1>0){a1* variables[i-1, "Movement"]}else{a0})
variables[i, "AIpapers"] <- (if(i-1>0){variables[i-1, "AIpapers"]}else{b0}) + (if(i-3>0){b1*variables[i-3, "Movement"]}else {0})
}
return(variables)
}
#This function works:
indeffbasic(a1=10, b1=2)
#Since I want a1 and b1 to be randomly generated each time,
#I define a function that randomly generates these values and returns them
genpar <- function() {
a1 <- rnorm(1, 1.1, 0.02)
b1 <- rnorm(1)
parameters <- c(a1, b1)
return(parameters)
}
#This function also seems to work
genpar()
#Now I define a function that randomly generates a1 and b1
#and then passes them to the indeffbasic function I defined above
#so each time I call this is like a run of the simulation.
indeff <- function(a0=5000, b0=100) {
parameters <- as.list(c(a0, b0, genpar()))
names(parameters) <- c("a0", "b0", "a1", "b1")
return(do.call(indeffbasic(), parameters))
}
#But this doesn't work: it returns "Error: argument "a1" is missing, with no default"
indeff()
'genpar'는 ('c'때문에) 단일 벡터를 반환합니다. 'c (a1, b1)'를'list ("a1 = a1,"b1 "= b1)'로 바꾸고 a1과 b1을 추출하여 명명 된 목록을 반환하게 할 수 있습니다. – lmo
감사합니다. @lmo, 네 말이 맞아. – Oldmantax