如何使在 ddply 中创建的对象在函数外部(在全局环境中)可用?
How can I make an object created within ddply available outside the function (in the global environment)?
我正在尝试 运行 对数据集中的多个个体主题进行 glm 以 return 特定系数。我在这里 (Multiple glm in for loop) 找到了一个非常有效的示例。除了,结果打印到屏幕上但之后不可用。因此,我无法保存结果或重命名变量,除非我在 RStudio 中突出显示屏幕然后 copy/paste 进入 Excel.
数据:
Subject SNRs Prop_Correct Ntrials
1 3 0.65 100
1 0 0.40 100
1 -3 0.15 100
1 -6 0.00 100
1 -9 0.00 100
1 -12 0.00 100
2 3 0.65 100
2 0 0.40 100
2 -3 0.15 100
2 -6 0.00 100
2 -9 0.00 100
2 -12 0.00 100
3 3 0.65 100
3 0 0.40 100
3 -3 0.15 100
3 -6 0.00 100
3 -9 0.00 100
3 -12 0.00 100
我的脚本:
ddply(Data, .(Subject), function (x){
intercept <- coef(summary(glm(Prop_Correct~SNRs, weights=Ntrials,family=quasibinomial(link='logit'),data=x)))[1]
slope <- coef(summary(glm(Prop_Correct~SNRs, weights=Ntrials,family=quasibinomial(link='logit'),data=x)))[2]
SNR50 <- (log(0.5/(1-0.5))/slope) - (intercept/slope)
Data_Summary <- c(SNR50,slope)
})
这给了我这个输出:
Subject V1 V2
1 1 1.266165 0.4834356
2 2 1.266165 0.4834356
3 3 1.266165 0.4834356
但是:
Data_Summary
Error: object 'Data_Summary' not found
如何使 ddply 函数的结果可用于 main/global 环境?
将结果赋给一个变量:
Data_Summary_df <- ddply(Data, .(Subject), function (x){
intercept <- coef(summary(glm(Prop_Correct~SNRs, weights=Ntrials,family=quasibinomial(link='logit'),data=x)))[1]
slope <- coef(summary(glm(Prop_Correct~SNRs, weights=Ntrials,family=quasibinomial(link='logit'),data=x)))[2]
SNR50 <- (log(0.5/(1-0.5))/slope) - (intercept/slope)
Data_Summary <- c(SNR50,slope)
})
Data_Summary_df
尽管发生了很多事情,但请将其视为任何函数调用,如果不分配给变量,它只输出结果,如果要保留结果,则将其分配给变量,例如
mean(1:25)
calc_mean <- mean(1:25)
calc_mean
我正在尝试 运行 对数据集中的多个个体主题进行 glm 以 return 特定系数。我在这里 (Multiple glm in for loop) 找到了一个非常有效的示例。除了,结果打印到屏幕上但之后不可用。因此,我无法保存结果或重命名变量,除非我在 RStudio 中突出显示屏幕然后 copy/paste 进入 Excel.
数据:
Subject SNRs Prop_Correct Ntrials
1 3 0.65 100
1 0 0.40 100
1 -3 0.15 100
1 -6 0.00 100
1 -9 0.00 100
1 -12 0.00 100
2 3 0.65 100
2 0 0.40 100
2 -3 0.15 100
2 -6 0.00 100
2 -9 0.00 100
2 -12 0.00 100
3 3 0.65 100
3 0 0.40 100
3 -3 0.15 100
3 -6 0.00 100
3 -9 0.00 100
3 -12 0.00 100
我的脚本:
ddply(Data, .(Subject), function (x){
intercept <- coef(summary(glm(Prop_Correct~SNRs, weights=Ntrials,family=quasibinomial(link='logit'),data=x)))[1]
slope <- coef(summary(glm(Prop_Correct~SNRs, weights=Ntrials,family=quasibinomial(link='logit'),data=x)))[2]
SNR50 <- (log(0.5/(1-0.5))/slope) - (intercept/slope)
Data_Summary <- c(SNR50,slope)
})
这给了我这个输出:
Subject V1 V2
1 1 1.266165 0.4834356
2 2 1.266165 0.4834356
3 3 1.266165 0.4834356
但是:
Data_Summary
Error: object 'Data_Summary' not found
如何使 ddply 函数的结果可用于 main/global 环境?
将结果赋给一个变量:
Data_Summary_df <- ddply(Data, .(Subject), function (x){
intercept <- coef(summary(glm(Prop_Correct~SNRs, weights=Ntrials,family=quasibinomial(link='logit'),data=x)))[1]
slope <- coef(summary(glm(Prop_Correct~SNRs, weights=Ntrials,family=quasibinomial(link='logit'),data=x)))[2]
SNR50 <- (log(0.5/(1-0.5))/slope) - (intercept/slope)
Data_Summary <- c(SNR50,slope)
})
Data_Summary_df
尽管发生了很多事情,但请将其视为任何函数调用,如果不分配给变量,它只输出结果,如果要保留结果,则将其分配给变量,例如
mean(1:25)
calc_mean <- mean(1:25)
calc_mean