R:数学函数的非数字参数

时间:2015-06-07 16:46:21

标签: r

我编写了一个用户定义的函数:

epf <- function(z,x,noise=std_noise){
    z_dims <- length(z)
    std_noise <- 0.5*matrix(1,1,z_dims)
    std_noise <- as.data.frame(std_noise)

    obs_prob <- dnorm(z,x[1:z_dims],noise)

    error <- prod(cbind(1,obs_prob))
    return(error)
}

在另一个函数的for循环中调用此函数:

w <- matrix(0,N,1)
        for (i in 1:N){
            w[i] <- epf(z,p[i,],R_noise) 
        }

其中z是二维向量,N=1000p是1000个观测值和4个变量的数据帧,R_noise是数据帧og 1观测值,4变量

这里我得到错误:“数学函数的非数字参数”,对于行obs_prob <- dnorm(z,x[1:z_dims],noise)

任何人都可以帮我找到错误吗?

我查看了类似于我的问题,但我仍无法在代码中找到错误。

编辑:
添加了N

的定义

1 个答案:

答案 0 :(得分:2)

================================================================= ==11642==ERROR: AddressSanitizer: heap-buffer-overflow on adress 0x61400000ffd4 at pc 0x47b8e5 bp 0x7fff67190bb0 sp 0x7fff67190ba8 READ of size 4 at 0x61400000ffd4 thread T0 #0 0x47b8e4 in main /home/pse/dockerfiles/memory_analysis_addressSanitizer/./main.cpp:5 #1 0x7f78b6c8bec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4) #2 0x47b44c in _start (/home/pse/dockerfiles/memory_analysis_addressSanitizer/a.out+0x47b44c) 0x61400000ffd4 is located 4 bytes to the right of 400-byte region [0x61400000fe40,0x61400000ffd0) allocated by thread T0 here: #0 0x465aa9 in operator new[](unsigned long) (/home/pse/dockerfiles/memory_analysis_addressSanitizer/a.out+0x465aa9) #1 0x47b76e in main /home/pse/dockerfiles/memory_analysis_addressSanitizer/./main.cpp:3 #2 0x7f78b6c8bec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4) SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pse/dockerfiles/memory_analysis_addressSanitizer/./main.cpp:5 main Shadow bytes around the buggy address: 0x0c287fff9fa0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c287fff9fb0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c287fff9fc0: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00 0x0c287fff9fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c287fff9fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c287fff9ff0: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa 0x0c287fffa000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c287fffa010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c287fffa020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c287fffa030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c287fffa040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Heap right redzone: fb Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack partial redzone: f4 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 ASan internal: fe ==11642==ABORTING 可能会更好。

更广泛地说,具有一行和两列的数据帧可以更好地表示为向量。数据框看起来像矩阵,当你把它作为“二维向量”时,它们在重要方面是不同的。

可能会发生同样的错误,因为您通过传递dnorm(as.matrix(z), x[1:a_dims], noise)在其最后一个参数dnorm中为noise提供了第二个数据框。

另外,请考虑R_noise有四个值。它由p[i, ]obs_prob进行子集化。在这种情况下,x[1:z_dims]将等于2,因为z_dims为2.所以您正在评估length(z)