引导以估计几何样本的平均值

时间:2015-10-13 21:08:37

标签: r

所以我需要为群体的几何平均值运行引导带方法。 6次测量是1,2,2,4,6,6,并且基于该样本的总体几何平均值的估计是gm =(1 * 2 * 2 * 4 * 6 * 6)*(1/6)。我需要计算总体几何平均值的95%置信限。

到目前为止,我有:

 set.seed(13254)
 gmsample <- c(1,2,2,4,6,6)
 gmsample
 n<- length(gmsample)
 gm.hat <- prod(gmsample) ** (1/6)
 gm.hat

 for(b in 1:B){
    inx.boot<- sample(1:n, replace=TRUE)

 gmboot<- gmsample[idx.boot]
 print(gmboot)

 rboot[b] <- prod(gmboot) ** (1/n)
 print(r.boot)
              }

 boot.sd <- sd(r.boot)
 boot.sd

我从互联网收集信息得到了这个,而且我对R非常新,所以任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

我建议使用启动包而不是滚动自己的bootstrapping方法。例如,计算1000几何均值的自举复制可以通过以下方式完成:

boot.ci

现在,您可以使用boot.ci(b, 0.95, "perc") # BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS # Based on 1000 bootstrap replicates # # CALL : # boot.ci(boot.out = b, conf = 0.95, type = "perc") # # Intervals : # Level Percentile # 95% ( 1.701, 4.670 ) # Calculations and Intervals on Original Scale 方法计算置信区间。例如,如果您想使用95%百分位置信区间,您可以这样做:

?boot.ci

还有许多其他类型的置信区间,您可以使用#include <unistd.h> #include <iostream> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #include <string.h> #include <fstream> #include <iomanip> using namespace std; int main (int argc, char **argv) { int sock, cli, read_size, file_size; //cli socket descriptor struct sockaddr_in server, client; char *send_buffer; char *fname; unsigned int sock_len; ofstream fout; cout << "Initializing a Socket\n"; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket: "); close(sock); exit(-1); } server.sin_family = AF_INET; server.sin_port = htons(7246); server.sin_addr.s_addr = INADDR_ANY; bzero(&server.sin_zero, 0); sock_len = sizeof(struct sockaddr); cout << "Binding a Socket\n"; if((bind(sock, (struct sockaddr *)&server, sock_len)) == -1) { perror("Bind: "); close(sock); exit(-1); } cout << "Listening\n"; if((listen(sock, 5)) == -1) { perror("Listen: "); } //To accept connection and get data from server while(1) { cout << "Waiting..."; if((cli = accept(sock, (struct sockaddr *)&client, &sock_len )) == -1) { perror("Accept: "); close(sock); exit(-1); } else { cout << "Server: Got connection from " << inet_ntoa(client.sin_addr) << endl; file_size = recv(cli, fname, 100, 0); cout << "\nOpening file\n"; fout.open(fname, ios::out | ios::app | ios::binary); if(!fout) { cout << "Error opening the file " <<fname << endl; close(sock); exit(1); } } cout << "Start now\n"; //while((recv(cli, send_buffer, sizeof(send_buffer), 0)) != EOF) //while(*send_buffer != EOF) while(strcmp(send_buffer, "_EOF_") != 0) { recv(cli, send_buffer, 10, 0); cout << "Receiving Msg..."; fout.write(send_buffer, sizeof(send_buffer)); /*if(*send_buffer == EOF) { cout << "Data Received\n"; close(sock); break; }*/ } //fout.write(EOF, sizeof(EOF)); /*if(read_size == 0) { cout << "Client Disconnected"; } else if(read_size == -1) { perror("Receive Failed"); close(sock); }*/ // fout.write(eof, sizeof(eof())); fout.close(); } close(sock); return 0; } 来阅读。