我的R代码:
bnc1<-function(maxITR=100000, d=2, l=1){
counts=0;
for (i in 1:maxITR){
x=runif(1,0,pi);
y=runif(2,0,d/2);
if ((l/2*sin(x)) >= y) counts=counts+1;
}
counts/maxITR*d/l
}
Run Code Online (Sandbox Code Playgroud)
运行代码:
> bnc1(maxITR=1000)
[1] 0.652
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In if ((l/2 * sin(x)) >= y) counts = counts + 1 ... :
the condition has length > 1 and only the first element will be used
2: In if ((l/2 * sin(x)) >= y) counts = …Run Code Online (Sandbox Code Playgroud) 我想把公式放在随机模型中,但我认为跟随错误是由于错误的公式对象(?),但无法修复它.
set.seed(1234)
mydata <- data.frame (A = rep(1:3, each = 20), B = rep(1:2, each = 30),
C = rnorm(60, 10, 5))
mydata$A <- as.factor(mydata$A)
mydata$B <- as.factor(mydata$B)
myfunction <- function (mydata, yvars, genovar, replication) {
require("lme4")
formula = paste ("yvars" ~ 1|"genovar" + 1|"replication")
model1 <- lmer(formula, data = dataframe, REML = TRUE)
return(ranef(model2))
}
myfunction(mydata=dataf, yvars = "C", genovar = "A", replication = "B")
Run Code Online (Sandbox Code Playgroud)
错误:长度(公式< - as.formula(公式))== 3不为TRUE
如果我有一个数组定义为:
Q <- array(runif(48), c(4,4,3), dimnames=list(
c("P","PO","C","T"), c("LL","RR","R","Y"), c("Jerry1", "Jerry2", "Jerry3")))
Run Code Online (Sandbox Code Playgroud)
我想要做的是检查地址Q [1,4]的数组Q中的每个"Jerry_n"矩阵,如果Q [1,4]小于Q [1,1],那么我会喜欢删除那个Jerry矩阵.
非常感谢您提供任何帮助.
我试图想象DiD模型的每个变量/组合的重要性.
attach(mtcars)
M=lm(mpg ~ hp + wt * gear , data =mtcars)
summary(M)
coef(M)
confint(M, level = 0.9)
Run Code Online (Sandbox Code Playgroud)
因此,我想创建一个条形图,以按排序顺序列出(大多数)重要系数.该summary(M)命令列出系数和相应的重要代码.具有高显着性代码(***)的每个系数应首先被监听,然后是**然后是*.点和''不应包括在内.
因此,首先,我如何获得每个系数的相应重要性代码/值.如何根据重要性包含/排除系数?
我正在尝试从R中的probit获取得分向量(对数似然的一阶导数)。这是我的示例代码:
library(AER) # Affairs data
data(Affairs)
mydata<-Affairs
mydata$affairs<-with(mydata,ifelse(affairs>0,1,affairs)) # convert to 1 and 0
glm(affairs ~ gender+ age + yearsmarried + children +
religiousness+education + rating,
family = binomial(link = "probit"),data = mydata)
Call: glm(formula = affairs ~ gender + age + yearsmarried + children +
religiousness + education + rating, family = binomial(link = "probit"),
data = mydata)
Coefficients:
(Intercept) gendermale age yearsmarried childrenyes religiousness education rating
0.76416 0.18882 -0.02440 0.05461 0.20807 -0.18609 0.01551 -0.27271
Degrees of Freedom: 600 …Run Code Online (Sandbox Code Playgroud) 我几乎可以用任何语言编写以下代码.subtractPrevious获取一个数组并i+1从ith值中减去st 值.如何使用R进行此类计算?这似乎是一项工作lapply或可能是一项dplyr功能.
x <- c(1,2,3,4,5,6,7,8,9,10)
subtractPrevious <- function(x){
for (i in 1:length(x)){
if (i == 1) {
y[1] <- NA
} else {
y[i] <- x[i] - x[i-1]
}
}
return(y)
}
y <- subtractPrevious(x)
Run Code Online (Sandbox Code Playgroud) 说我有以下数据帧
x <- c("p1","p2","p3","p4","p5","p6","p7","p8","p9","p10")
y <- c(1,4,3,5,5,7,2,2,6,8)
df <- data.frame(x,y)
Run Code Online (Sandbox Code Playgroud)
说x代表球员并y代表目标.比如说,我想要所有目标总和为10的玩家子集
{p1,p3,p9},{p3,p6},{p7,p8,p9}...
Run Code Online (Sandbox Code Playgroud) a <- c(1,2,3)
b <- c(1,2)
corr <- cor(a,b)
Run Code Online (Sandbox Code Playgroud)
我有两个时间序列变量,想要计算相关性,但它们有不同的样本量。为了简化我的问题,考虑是否有两个变量a,b并且我想计算 和 之间的相关性a,b但我只想计算前两个值。我如何在 R 中实现这一目标?
我有一个数据集,其中包含参与者 IDS 和每个参与者的 17 种不同度量。
我需要删除离群值 - 与两侧均值相差 3 个标准差的数字。这需要对每一列单独进行。
到目前为止,通过使用下面的代码,我已经成功地将 NA 添加到每列的离群值列中,但这对我没有多大帮助,因为我需要能够将 NA 添加到带有其余数字的列中或者简单地删除异常值
理想情况下,我想要访问一个如下所示的文件:
ID measure1 measure2 ....measure17
1 10897 64436
2 184658 1739473
3 75758
4 746483 4327349 3612638
5 6444 36363 46447
Run Code Online (Sandbox Code Playgroud)
到目前为止我使用过的代码:
phenotypes <- colnames(imaging_data_kept[,2:ncol(imaging_data_kept)])
for (i in phenotypes){
Min <- mean(imaging_data_kept[[i]]) - (3*sd(imaging_data_kept[[i]]))
Max <- mean(imaging_data_kept[[i]]) + (3*sd(imaging_data_kept[[i]]))
imaging_data_kept[[paste0(i,"_outliers")]] <- imaging_data_kept[[i]] <
Min | imaging_data_kept[[i]] > Max
}
Run Code Online (Sandbox Code Playgroud)
样本数据:
SubjID M1 M2 M3 M4 M5
1000496 14898.1 9172 4902 5921.9 1428.2
1001121 5420.7 2855.5 …Run Code Online (Sandbox Code Playgroud)