【发布时间】:2018-02-07 17:19:49
【问题描述】:
首先,我有你的向量,
(x <- c(7.2, 8.6, 9.1, 9.5, 10.9, 10.9, 11.1, 11.5, 11.7,
11.9, 11.9, 12.7, 12.9, 13.9, 14.1, 14.5, 14.7))
#> [1] 7.2 8.6 9.1 9.5 10.9 10.9 11.1 11.5 11.7 11.9 11.9 12.7 12.9 13.9 14.1 14.5
#> [17] 14.7
(qjx <- c(6.12, 7.90, 8.85, 9.30, 10.20, 10.90, 11.00, 11.30, 11.60,
11.80, 11.90, 12.30, 12.80, 13.40, 14.00, 14.30, 14.60, 17.42))
#> [1] 6.12 7.90 8.85 9.30 10.20 10.90 11.00 11.30 11.60 11.80 11.90 12.30 12.80
#> [14] 13.40 14.00 14.30 14.60 17.42
(rjx <- c(1.78, 0.95, 0.45, 0.9, 0.7, 0.1, 0.3, 0.3,
0.2, 0.1, 0.4, 0.5, 0.6, 0.6, 0.3, 0.3, 2.82))
#> [1] 1.78 0.95 0.45 0.90 0.70 0.10 0.30 0.30 0.20 0.10 0.40 0.50 0.60 0.60 0.30 0.30
#> [17] 2.82
x.seq 是 x 的平滑向量,Fm 是确定向量 x 的 c.d.f 值的函数
x.seq <- seq(qjx[1],qjx[m+1],by=0.01)
Fm <- function(data,x){ #data = smooth data, x = intial data
x.smooth <- NULL;
xsmooth <- matrix(,nrow=(length(x)),ncol=length(data))
xsmooth. <- matrix(,nrow=(length(x)),ncol=length(data))
for(i in 1:m){
for(j in 1:length(data)){
xsmooth[i,j] <- (data[j] - qjx[i])/rjx[i]
if(xsmooth[i,j] < 0){
xsmooth.[i,j] <- 0
} else
if(xsmooth[i,j] >= 0 && xsmooth[i,j] <= 1){
xsmooth.[i,j] = xsmooth[i,j]
} else
if(xsmooth[i,j] > 1){
xsmooth.[i,j] <- 1
}
}
}
xsmoothing <- NULL
for(k in 1:ncol(xsmooth.)){
xsmoothing[k] <- mean(xsmooth.[,k])
}
print(as.numeric(xsmoothing))
}
接下来我想用下面的函数从函数Fm 计算t 的逆值
m <- length(x)
Finv <- function(t){
if(t > 0){
f.inv <- matrix(,ncol=2,nrow=m)
k <- 1;f.invers <- NULL
while(k <= m){
f.inv[k,1] <- (k-1)/m
f.inv[k,2] <- k/m
if(t >= f.inv[k,1] && t <= f.inv[k,2]){
f.invers[k] <- (((m*t)-(k-1))*rjx[k])+qjx[k]
} else
f.invers[k] <- NA
k <- k+1
}
hasil <- as.numeric(f.invers[!is.na(f.invers)])
print(hasil)
} else
hasil <- L # L = the initial value,for example L is 2.06
print(as.numeric(hasil))
}
之所以做这个函数,是因为有很多t的序列。所以,我为每个 q 从 1 到最后一个顺序编译 t 的向量
t <- Fm(x.seq,x)
invers <- NULL
for(q in 1:length(t)){
print(invers[q] <- Finv(1-t[q]))
}
invers
正常工作,但是在处理invers的值时,有警告
Warning messages:
1: In invers[q] <- Finv(1 - t[q]) :
number of items to replace is not a multiple of replacement length
2: In invers[q] <- Finv(1 - t[q]) :
number of items to replace is not a multiple of replacement length
3: In invers[q] <- Finv(1 - t[q]) :
number of items to replace is not a multiple of replacement length
4: In invers[q] <- Finv(1 - t[q]) :
number of items to replace is not a multiple of replacement length
5: In invers[q] <- Finv(1 - t[q]) :
number of items to replace is not a multiple of replacement length
6: In invers[q] <- Finv(1 - t[q]) :
number of items to replace is not a multiple of replacement length
我只想知道我的反函数的错误在哪里。谢谢
【问题讨论】:
-
invers[q] <- Finv(1 - t[q])中发生了两件事:您正在调用函数Finv并将结果分配给列表invers[q] <-。问题是 R 没有构建这样的列表,请参阅 this question for example。 -
@JesseTweedle 我试图将列表中的结果更改为
invers[[q]] <- Finv(1 - t[q])及其工作,谢谢。但在分析中,我需要一个向量来表示结果。怎么改成向量? -
请看下面的答案。如果你想要一个向量(但你的函数需要向量化),通常 R 喜欢一次将函数应用于整个向量。如果您没有矢量化函数,另一种解决方案是使用
purrr::map,例如purrr::map(1 - t, Finv)或其他东西。 -
抱歉,
Finv函数的某处也有问题。很难找到,但Finv(1 - t[179])返回[1] 14.6 14.6!两件事! -
@JesseTweedle 是的,这也是我关心的问题。但最后我使用
invers[[q]]并使用c(do.call("cbind",invers))然后使用unique()擦除双数据。所以我的逆向量与t的长度相同。