我想模拟相关的分类数据和连续数据。如何在 R 中实现这一点?
#For example, how to simulate the data in a way that these two variable are correlated?
x <- sample( LETTERS[1:4], 1000, replace=TRUE, prob=c(0.1, 0.2, 0.65, 0.05) ) #Categorical variable
y <- runif(1000,1,5) #Continuous variable
Run Code Online (Sandbox Code Playgroud)
任何想法将不胜感激!
我正在尝试计算数据集中一组列的点双列相关性。我可以对单个变量执行此操作,但是如果我需要在一次迭代中计算所有列,则会显示错误。
下面是代码:
df = pd.DataFrame({'A':[1, 0, 1, 0, 1], 'B':[6, 7, 8, 9, 10],'C':[9, 4, 6,9,10],'D':[8,9,5,7,10]})
from scipy import stats
corr_list = {}
y = df['A'].astype(float)
for column in df:
x = df[['B','C','D']].astype(float)
corr = stats.pointbiserialr(x, y)
corr_list[['B','C','D']] = corr
print(corr_list)
TypeError: No loop matching the specified signature and casting was found for ufunc add
Run Code Online (Sandbox Code Playgroud) 我对这里的问题还有一个后续问题。
This person wanted to make a correlation plot with ggcorrplot from the package ggcorrplot. However, they wanted to have the diagonal going down the matrix instead of up from left to right. So, they wanted to make the graph look like the correlation matrix that they used as input:
library(ggcorrplot)
data(mtcars)
corr.mat <- cor(mtcars[, c("mpg", "disp", "hp", "drat", "wt", "carb")])
ggcorrplot(corr.mat)
print(corr.mat)
Run Code Online (Sandbox Code Playgroud)
The following solution was given, which works fine, as long as you use the specification type …
我正在做一个 NLP 项目,研究 10 位不同经典摇滚艺术家各自作品之间的余弦相似度。我已经完成了相似性查询并创建了一个名为的数据框,similarities如下所示。
我similarities通过首先构建一个空数据框来构建数据框,其中以艺术家姓名作为索引和列名称,然后运行以下代码来运行相似性查询并使用分数填充数据框:
artist_words = data['lyrics'][artist]
artist_vec_bow = dictionary.doc2bow(artist_words.lower().split())
artist_vec_lsi = lsi[artist_vec_bow]
artist_sims = index[artist_vec_lsi]
artist_sims_sorted = sorted(enumerate(artist_sims), key=lambda item: -item[1])
for position, score in artist_sims_sorted:
similarities.at[artist, musicians[position][1]] = score
Run Code Online (Sandbox Code Playgroud)
data是一个数据框,以艺术家姓名作为索引,并有一列名为 ,lyrics其中歌词作为一个长字符串输入。
我想创建一个 Seaborn 热图来可视化 DataFrame 中指示的相关性。
但是当我运行以下代码时:
sns.heatmap(similarities)
我收到一条很长的错误消息,其结尾为:
AttributeError: 'NoneType' object has no attribute 'reshape'
谁能帮我弄清楚如何形象化这一点?
similarities.info()返回以下内容:
Index: 11 entries, bob_dylan to willie_nelson
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- ----- …Run Code Online (Sandbox Code Playgroud) 我做了部分相关分析,与ggm包
list = list(mtcars, mtcars)
list = lapply(list, function(x) x %>%
mutate(gear = as.factor(gear)))
library(ggm)
lapply(list, function(x) {
sapply(split(x, x$gear), function(x) {
pcor(u = c('mpg', 'disp', 'hp', 'vs'), S = var(x))
})
})
Run Code Online (Sandbox Code Playgroud)
pcor和包装一起
pcorr1 = list %>%
map(function(x) split(x[c('mpg', 'disp', 'hp', 'vs')], x$gear))
coeff = c("pearson", "spearman")
res = lapply(1:2, function(x) lapply(seq(coeff), function(x) {
lapply(pcorr1[[x]], function(y) pcor(y, method = coeff[[x]]))}))
Run Code Online (Sandbox Code Playgroud)
任何人都可以推荐一种如何使用 ggplot2 计算图中的这种相关性的方法吗?
谢谢
UPFATE 只是为了理解,我想知道是否可以使用相关系数作为 y 和 x 所有级别的分组变量(它应该是一种条形图)
我在时域中有两个波形,我需要在MATLAB中测量互相关系数.我试过max(abs(xcorr(m,n,'coeff')))但它似乎没有正常工作.
另外,我需要测量波形的不同部分的互相关系数,例如以1分钟的间隔测量互相关系数.如果可能的话,将这些值输出到矩阵或其他东西.
我知道这是一个很多问题,但我是一个MATLAB新手,发现这个任务令人生畏!
我将非常感激地向您提供有关此问题任何部分的任何帮助.
编辑: 这是我用来测试相关代码的代码:
x = rand(1,14400);
y = rand(1,14400);
r = max( abs(xcorr(x,y,'coeff')) )
Run Code Online (Sandbox Code Playgroud) 我有一个data.frame,我想用一列相对于其他列(框架中也有一些非数字列)来计算相关系数。
ddply(Banks,.(brand_id,standard.quarter),function(x) { cor(BLY11,x) })
# Error in cor(BLY11, x) : 'y' must be numeric
Run Code Online (Sandbox Code Playgroud)
我针对is.numeric(x)进行了测试
ddply(Banks,.(brand_id,standard.quarter),function(x) { if is.numeric(x) cor(BLY11,x) else 0 })
Run Code Online (Sandbox Code Playgroud)
但是每次比较都失败,返回0,仅返回一列,就好像它仅被调用过一次一样。什么传递给该函数?刚来到R,我认为我缺少一些基本知识。
谢谢
我想绘制随时间步长的双变量相关性,以便x轴是时间,y轴是双变量相关系数.该airquality数据可以是一个很好的例子.在这种情况下,我想绘制之间的相关性Ozone&Temp和Ozone&Wind过度Day.谢谢!
data(airquality)
Run Code Online (Sandbox Code Playgroud)
相关矩阵如下所示:
Ozone Solar.R Wind Temp Month Day
Ozone 1.00 0.35 -0.60 0.70 0.16 -0.01
Solar.R 0.35 1.00 -0.06 0.28 -0.08 -0.15
Wind -0.60 -0.06 1.00 -0.46 -0.18 0.03
Temp 0.70 0.28 -0.46 1.00 0.42 -0.13
Month 0.16 -0.08 -0.18 0.42 1.00 -0.01
Day -0.01 -0.15 0.03 -0.13 -0.01 1.00
Run Code Online (Sandbox Code Playgroud) 我有一个数据框,我想找出哪一组变量共享最高的相关性。例如:
mydata <- structure(list(V1 = c(1L, 2L, 5L, 4L, 366L, 65L, 43L, 456L, 876L, 78L, 687L, 378L, 378L, 34L, 53L, 43L),
V2 = c(2L, 2L, 5L, 4L, 366L, 65L, 43L, 456L, 876L, 78L, 687L, 378L, 378L, 34L, 53L, 41L),
V3 = c(10L, 20L, 10L, 20L, 10L, 20L, 1L, 0L, 1L, 2010L,20L, 10L, 10L, 10L, 10L, 10L),
V4 = c(2L, 10L, 31L, 2L, 2L, 5L, 2L, 5L, 1L, 52L, 1L, 2L, 52L, 6L, 2L, 1L),
V5 = c(4L, 10L, 31L, 2L, …Run Code Online (Sandbox Code Playgroud) 我想在本质上使一列为布尔值之后计算熊猫数据帧的两列之间的相关系数。原来table有两列:一个Group包含两个处理组(现在为布尔值)之一的列和一个Age组。这些是我要计算相关系数的两列。
我尝试了以下.corr()方法:
table.corr(method='pearson')
Run Code Online (Sandbox Code Playgroud)
我已经在table下面粘贴了布尔值的前25行。我不知道是否缺少参数,或者如何解释此结果。也为1也很奇怪。提前致谢!
Group Age
0 1 50
1 1 59
2 1 22
3 1 48
4 1 53
5 1 48
6 1 29
7 1 44
8 1 28
9 1 42
10 1 35
11 0 54
12 0 43
13 1 50
14 1 62
15 0 64
16 0 39
17 1 40
18 1 59
19 1 46
20 0 56 …Run Code Online (Sandbox Code Playgroud) correlation ×10
r ×6
python ×3
pandas ×2
continuous ×1
ggcorrplot ×1
ggplot2 ×1
grouping ×1
heatmap ×1
matlab ×1
matplotlib ×1
partial ×1
plyr ×1
seaborn ×1
simulation ×1
time-series ×1