使用带有多个参数和索引的lapply / sapply减少函数内的嵌套循环

时间:2019-02-25 20:46:10

标签: r function for-loop lapply

我有两个观测向量,我想计算出最适合这些观测的参数。因此,我定义了似然函数。我正在努力从嵌套循环到使用sapply。我的目标是返回观察“预算”概率的(50x1)向量。

> bud
 [1] 1.72 1.12 1.01 1.14 1.56 1.04 1.00 1.39 1.38
[10] 0.99 1.66 1.55 2.26 0.86 1.45 0.90 1.58 1.09
[19] 0.97 0.98 1.16 1.20 1.17 1.59 1.00 0.92 1.24
[28] 0.98 1.29 1.36 1.80 1.42 1.67 1.13 0.76 0.93
[37] 1.03 1.29 1.09 1.64 1.27 1.14 1.60 1.00 0.91
[46] 1.40 1.15 2.03 1.34 1.37
> deltakere
 [1] 8 5 7 6 9 5 5 9 9 6 5 8 8 6 6 5 7 6 7 7 9 6 6 8
[25] 8 6 6 5 9 9 6 7 9 5 5 6 6 8 7 9 8 6 9 8 5 8 8 6
[49] 9 7

当前代码(尽管运行缓慢):

# first equation in my likelihood function
eqOne <- function(i,theta1, theta2, lambda){
        vektorN = (1:100)
        loopVerdi = 0
            if(deltakere[i]>=2){        
                for (N in deltakere[i]:20000) {
                    density = ((N)*(N-1)*(pWEI2(bud[i], theta1, theta2))^(N-2) *
                        pWEI2(bud[i], theta1, theta2, lower.tail = FALSE) *
                        dWEI2(bud[i], theta1, theta2)) /
                        (1-(pWEI2(r, theta1, theta2))^N)
                    ngittN =
                        dbinom(deltakere[i], size = N, 
                               prob = pWEI2(r, theta1, theta2, lower.tail = FALSE))
                    sN = 
                        dpois(N, lambda)
                    loopVerdi = loopVerdi + (density * ngittN * sN)}
            return(loopVerdi)}}

# here is the second equation in the likelihood estimate.
eqTwo <- function(theta1, theta2, lambda) {
        firstPart <- 1:length(bud)
            for (i in 1:length(bud)){
                firstPart[i] <- eqOne(i,theta1, theta2, lambda)}
        return(firstPart)}

Vector <- eqTwo(1,2,7)
> Vector
 [1] 0.071126958 0.174198273 0.125381705 0.202950594
 [5] 0.116409283 0.156245797 0.143071864 0.156875579
 [9] 0.157670857 0.139149430 0.052085368 0.138248017
[13] 0.001884680 0.073326986 0.161989881 0.103352207
[17] 0.125124465 0.186201434 0.103156346 0.108636007
[21] 0.116075468 0.214339615 0.209921459 0.121433257
[25] 0.084230719 0.102485943 0.216127393 0.135710730
[29] 0.153834163 0.158552633 0.036673600 0.194744214
[33] 0.079498794 0.175511921 0.049722001 0.107646750
[37] 0.159465393 0.198467993 0.169062724 0.089375280
[41] 0.196173239 0.202950594 0.102923165 0.084230719
[45] 0.107521392 0.190136547 0.159243477 0.008274964
[49] 0.158457042 0.210361176

此代码产生一个(50x1)向量,具有观察“预算”值的可能性,这正是我想要的。

这是我尝试从for循环过渡到适用的方法

funDeltakere <- function(i, theta1, theta2, lambda){ 
    function(N, theta1, theta2, lambda){
                    density = ((N) *(N-1)*(pWEI2(bud[i], theta1, theta2))^(N-2) *
                        pWEI2(bud[i], theta1, theta2, lower.tail = FALSE) *
                        dWEI2(bud[i], theta1, theta2)) /
                        (1-(pWEI2(r, theta1, theta2))^N)
                    ngittN =
                        dbinom(deltakere[i], size = N, 
                               prob = pWEI2(r, theta1, theta2, lower.tail = FALSE))
                    sN = dpois(N, lambda)
                    return(density * ngittN * sN)}}

eqOne <- function(i,theta1, theta2, lambda){
            if(deltakere[i]>=2){        
                loopVerdi <- sapply(deltakere[i]:20000, funDeltakere(i, theta1, theta2, lambda))
            return(loopVerdi)}} 

eqTwo <- function(theta1, theta2, lambda) {
            firstPart <- 1:length(bud)
                for (i in 1:length(bud)){
                    firstPart[i] <- eqOne(i,theta1, theta2, lambda)
                }
            return(firstPart)
            }
Vector <- eqTwo(1,2,7)
  

pWEI2(bud [i],theta1,theta2)中的错误:参数“ theta1”为   缺少,没有默认值

似乎参数并没有完全通过代码中的第一个函数传递。关于如何确保我的参数传递给所有功能的任何提示将不胜感激!

1 个答案:

答案 0 :(得分:0)

我设法解决了问题。

\documentclass[xcolor={table}]{beamer}

\usepackage{pgf}

%new color
\definecolor{Gray}{gray}{0.9}
\def\textboxcolor{Gray}
%----------------------------------------------------------------------
%----------------------------------------------------------------------
\setbeamertemplate{headline}{%
\hspace{0.01\textwidth}\begin{minipage}[t]{0.88\textwidth}
\vspace{0.1cm}
\begin{tabular}{p{0.5\textwidth}p{0.4\textwidth}p{0.1\textwidth}}
Organisation - Institution&%
&%
\includegraphics[width=1cm]{example-image-duck}\\
\hline\\
\end{tabular}
\end{minipage}}
%%----------------------------------------------------------------------
\setbeamertemplate{footline}{%
\hspace{0.01\textwidth}\begin{minipage}[t]{0.88\textwidth}%
\begin{tabular}{p{0.3\textwidth}p{0.35\textwidth}p{0.2\textwidth}p{0.1\textwidth}}%
\rowcolor[rgb]{0.8,0.8,0.8}\hline%
\raggedright\begin{minipage}{0.3cm}\includegraphics[width=0.30cm]{example-image}\end{minipage}\ \insertauthor &%
\centering \    \usebeamerfont{date in head/foot}\insertshortdate{}& &%
Seite \insertframenumber{}/\inserttotalframenumber\\
\hline\\
\end{tabular}
\end{minipage}}

\begin{document}

\begin{frame}{Normal Frame}
    \begin{block}{Blocktitle}
    Text
  \end{block}
\end{frame}

\begingroup
    \setbeamertemplate{navigation symbols}{%
        \color{black}%
      \tiny%
      \setbeamertemplate{page number in head/foot}[totalpagenumber]%
      \usebeamertemplate{page number in head/foot}%
    }
    \begin{frame}[plain]{Frame with Images}
        \centering
        \includegraphics[height=0.5\textheight]{example-image}%
        \includegraphics[height=0.5\textheight]{example-image}

        \includegraphics[height=0.5\textheight]{example-image}%
        \includegraphics[height=0.5\textheight]{example-image}
    \end{frame}
\endgroup

\end{document}