问题列表 - 第318123页

KM 估计的差异:svykm 与 survfit 带权重

我正在病例对照研究中用权重来估计生存率。因此,所有案例的权重都等于 1。在绘制估计曲线并将其与未加权估计进行比较时,我注意到案例的 KM 曲线不重叠。

这是“survival”包中的数据代码。

library(dplyr)
library(tidyverse)
library(survival)
library(broom)
library(WeightIt)
library(survey)


a <- survival::ovarian

#Calculation of weghts:

weights <- WeightIt::weightit(rx ~ age + ecog.ps + resid.ds, int = T, estimand = "ATT", data = a, method = "glm" , stabilize = F, missing = "saem") 
a$weights <- weights$weights
a$ps <- weights$ps

design <- svydesign(ids = ~ 1, data = a, weights = ~weights)

KM_PFS <- survfit(Surv(futime, fustat > 0)~rx, a) # KM naive 
KM_PFS_w_TT <- survfit(Surv(futime, fustat > 0)~rx, a, …
Run Code Online (Sandbox Code Playgroud)

r survey survival

4
推荐指数
1
解决办法
62
查看次数

如何根据R中其他变量的类别汇总值?

我有一个数据集,显示了X国甲方和乙方的宗教信仰,以及每个国家宗教信徒的百分比。

df <- data.frame(
  PartyA = c("Christian","Muslim","Muslim","Jewish","Sikh"),
  PartyB = c("Jewish","Muslim","Christian","Muslim","Buddhist"),
  ChristianPop = c(12,1,74,14,17),
  MuslimPop = c(71,93,5,86,13),
  JewishPop = c(9,2,12,0,4),
  SikhPop = c(0,0,1,0,10),
  BuddhistPop = c(1,0,2,0,45)
)
#      PartyA    PartyB ChristianPop MuslimPop JewishPop SikhPop BuddhistPop
# 1 Christian    Jewish           12        71         9       0           1
# 2    Muslim    Muslim            1        93         2       0           0
# 3    Muslim Christian           74         5        12       1           2
# 4    Jewish    Muslim           14        86         0       0           0
# 5      Sikh  Buddhist           17        13         4      10          45
Run Code Online (Sandbox Code Playgroud)

借此,我想将“参与”的宗教信徒的总数加在一起。因此,第一行将得到一个等于 …

r dataframe

4
推荐指数
1
解决办法
139
查看次数

Generic way for traversing a C++ std::tuple

My goal is to have a generic way to traverse a std::tuple, and the following code tries to show it:

#include <iostream>
#include <tuple>

template <typename t, size_t t_idx, typename t_tuple>
concept visit_tuple_element_value = requires(t &&p_t, const t_tuple &p_tuple) {
  {
    p_t.template operator()<t_idx>(
        std::declval<std::add_const_t<std::add_lvalue_reference_t<t_tuple>>>())
    } -> std::same_as<bool>;
};

template <typename t_tuple, typename t_function, size_t t_idx = 0>
requires(visit_tuple_element_value<t_function, t_idx, t_tuple>)

    void traverse_tuple_values(t_function p_function, const t_tuple &p_tuple) {
  if constexpr (t_idx < std::tuple_size_v<t_tuple>) {
    if (p_function.template operator()<t_idx>(p_tuple)) {
      traverse_tuple_values<t_tuple, t_function, …
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming c++-concepts

2
推荐指数
1
解决办法
112
查看次数

在Delphi中,在事件中释放发送者是正确的方法吗?

所以,假设我们有一个这样的类:

  TFieldsReadyEvent = procedure(Sender: TObject; Code, AuthorizatedID: string) of object;
  TCatcherServer = class(TServer)
  private
    FOnFieldsReady: TFieldsReadyEvent;
    FCode: string;
    FAuthoID: string;
    FAuthorizationType: TAuthorizationType;
    function GetAuthorizationType: string;
    function GetCode: string;
    function GetEntityID: string;
  public
    property OnFieldsReady: TFieldsReadyEvent read FOnFieldsReady write FOnFieldsReady;
    property Code: string read GetCode;
    property EntityID: string read GetEntityID;
    property AuthorizationType: string read GetAuthorizationType;
    procedure Callback(Session: TIdContext; Req: TIdHTTPRequestInfo; Res: TIdHTTPResponseInfo); override;
  end;
Run Code Online (Sandbox Code Playgroud)

程序Callback如下所示:

procedure TCatcherServer.Callback(Session: TIdContext;
  Req: TIdHTTPRequestInfo; Res: TIdHTTPResponseInfo);
begin
  inherited Callback(Session, Req, Res);
  if (Req.Params.Values['code'] …
Run Code Online (Sandbox Code Playgroud)

delphi pascal

2
推荐指数
1
解决办法
148
查看次数

Pandas 修改滚动平均值

下面是我在 pandas 中的异常值检测代码。我正在滚动 15 个窗口,我想要的是滚动 5 个窗口,其中该窗口基于居中日期的星期几,即如果中心是星期一,则在星期一向后移动 2 个,在星期一向前移动 2 个。Rolling 对此没有任何支持。怎么做?

import pandas as pd
import numpy as np

np.random.seed(0)

dates = pd.date_range(start='2022-01-01', end='2023-12-31', freq='D')

prices1 = np.random.randint(10, 100, size=len(dates))
prices2 = np.random.randint(20, 120, size=len(dates)).astype(float)

data = {'Date': dates, 'Price1': prices1, 'Price2': prices2}
df = pd.DataFrame(data)

r = df.Price1.rolling(window=15, center=True)
price_up, price_low = r.mean() + 2 * r.std(), r.mean()  -  2 * r.std()

mask_upper = df['Price1'] > price_up
mask_lower = df['Price1'] < price_low

df.loc[mask_upper, 'Price1'] = r.mean()
df.loc[mask_lower, 'Price1'] …
Run Code Online (Sandbox Code Playgroud)

python numpy dataframe pandas

2
推荐指数
1
解决办法
65
查看次数

PowerBI - Python 从邮政编码中提取城市、国家/地区

我在 PowerBI 仪表板中有一个名为“邮政编码”的专栏,我可以使用 PowerBI 中的 python 库编写脚本并提取城市、州和国家/地区名称吗?

这是我到目前为止所尝试过的,我使用 ODBC 连接到数据库。我有一个返回 49,445 行的示例查询

SELECT
ID,
POSTAL_CODE
FROM EMPLOYEE_DATA;
Run Code Online (Sandbox Code Playgroud)

这是我的 python 脚本,它是我的 PowerBI 报告的一部分

# 'dataset' holds the input data for this script

# Import the required library
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut

# Function to extract city, state, and country from postal code
def extract_location(POSTAL_CODE):
    geolocator = Nominatim(user_agent="geoapi")
    try:
        location = geolocator.geocode(POSTAL_CODE, timeout=10)  # Adjust the timeout value as needed
        if location:
            return location.address.split(", ")[-3:]
        else:
            return ["", …
Run Code Online (Sandbox Code Playgroud)

python postal-code powerbi-desktop

5
推荐指数
1
解决办法
44
查看次数

将向量元素重新排序为 R 中的第二个元素

假设我们有一些如下所示的二元素字符串向量A, B, C, ...。我们只知道保存为 object 的元素之一second

有没有办法使second始终成为 中的第二个元素A, B, C, ...

A = c("time","prof") # --> must become `c("prof", "time")`

B = c("prof", "time") # --> remains Unchanged

C = c("time","ziba") # --> must become `c("ziba", "time")`

second = "time"
Run Code Online (Sandbox Code Playgroud)

string r function vector

2
推荐指数
1
解决办法
61
查看次数

使用函数参数使用常量表达式作为模板参数:哪个编译器是正确的?

我有这段代码:

template <int V>
struct Constant {
    constexpr operator int() const noexcept { return V; }
};


template <class T, int N>
struct Array { };

auto function(auto s) -> Array<int, s + s> {
    return {};
}

auto const a = function(Constant<3>{});
Run Code Online (Sandbox Code Playgroud)

让我最悲伤的是,似乎只有 Clang 接受这个代码。

哪个编译器是正确的,为什么?

c++ templates language-lawyer constexpr c++20

5
推荐指数
1
解决办法
118
查看次数

在C++中,LONG和long有什么区别?(大小写区别)

Visual Visual Studio 中的 C++,“LONG”显示绿色,而“long”显示蓝色。

使用一个有什么区别吗?使用它有什么缺点吗?颜色有什么意义吗?

我只是对此感到好奇,但没有找到任何真正可以解释这一点的东西。

c++ types

0
推荐指数
1
解决办法
109
查看次数

使用 Python 下载 pdf 网址

我正在尝试从此网站下载 .pdf 网址 PDFLINK https://www.cell.com/heliyon/pdf/S2405-8440(18)33206-7.pdf 我可以从浏览器中查看它,但使用Python 的请求不允许我下载它。

我尝试了以下操作,但无法正确下载。它会返回 403 或说 .pdf 已损坏。但我可以很好地查看链接https://www.cell.com/heliyon/pdf/S2405-8440(18)33206-7.pdf 有什么想法吗?

import requests


def download_pdf(url):
    response = requests.get(url)
    with open('sample.pdf', 'wb') as f:
        f.write(response.content)


if __name__ == "__main__":
    url = "https://www.cell.com/heliyon/pdf/S2405-8440(18)33206-7.pdf"
    download_pdf(url)

Run Code Online (Sandbox Code Playgroud)

python pdf python-requests

1
推荐指数
1
解决办法
128
查看次数