correlation

how do i calculate correlation between corresponding columns of two matrices and not getting other correlations as output

廉价感情. 提交于 2019-12-04 05:12:21
I have these data > a a b c 1 1 -1 4 2 2 -2 6 3 3 -3 9 4 4 -4 12 5 5 -5 6 > b d e f 1 6 -5 7 2 7 -4 4 3 8 -3 3 4 9 -2 3 5 10 -1 9 > cor(a,b) d e f a 1.0000000 1.0000000 0.1767767 b -1.0000000 -1.000000 -0.1767767 c 0.5050763 0.5050763 -0.6964286 The result I want is just: cor(a,d) = 1 cor(b,e) = -1 cor(c,e) = 0.6964286 I would probably personally just use diag : > diag(cor(a,b)) [1] 1.0000000 -1.0000000 -0.6964286 But you could also use mapply : > mapply(cor,a,b) a b c 1.0000000 -1.0000000 -0.6964286 The first answer above calculates all pairwise correlations, which is fine unless the matrices

Pandas corr() returning NaN too often

丶灬走出姿态 提交于 2019-12-04 04:33:54
问题 I'm attempting to run what I think should be a simple correlation function on a dataframe but it is returning NaN in places where I don't believe it should. Code: # setup import pandas as pd import io csv = io.StringIO(u''' id date num A 2018-08-01 99 A 2018-08-02 50 A 2018-08-03 100 A 2018-08-04 100 A 2018-08-05 100 B 2018-07-31 500 B 2018-08-01 100 B 2018-08-02 100 B 2018-08-03 0 B 2018-08-05 100 B 2018-08-06 500 B 2018-08-07 500 B 2018-08-08 100 C 2018-08-01 100 C 2018-08-02 50 C 2018-08

R cor returns NaN sometimes

∥☆過路亽.° 提交于 2019-12-04 03:08:02
问题 I've been working on some data, available here: Dropbox' csv file (please be kind to use it to replicate the error). When I run the code: t<-read.csv("120.csv") x<-NULL for (i in 1:100){ x<-c(x,cor(t$nitrate,t$sulfate,use="na.or.complete")) } sum(is.nan(x)) I get random values of the last expression, usually around 55 to 60. I expect cor to give repetible results, so I expect x to be a vector of length=100 made of identical values. See, for example, the output of two independent runs: > x<

How to find correlation of an image?

送分小仙女□ 提交于 2019-12-04 02:47:07
问题 There is an image A of fixed size 256*256*3 (RGB). The mathematical formula for covariance between two adjacent pixels values x,y in an image is popularly known to be: cov(x,y) = 1/n summation from i = 1 to n of [E(x_i-E(x))(y_i-E(y))] r_xy = cov(x,y) / (sqrt(D(x)*D(y))) D(x) = 1/n summation from i = 1 to n of square[(x_i - E(x))] E(x) = 1/n summation from i = 1 to n of (x_i) where r_xy is the correlation coefficients between two horizontally, vertically, and diagonally adjacent pixels of

Intraclass Correlation in Python Module?

懵懂的女人 提交于 2019-12-04 02:11:22
I'm looking to calculate intraclass correlation (ICC) in Python. I haven't been able to find an existing module that has this feature. Is there an alternate name, or should I do it myself? I'm aware this question was asked a year ago on Cross Validated by another user, but there were no replies. I am looking to compare the continuous scores between two raters. Kenly You can find an implementation at ICC or Brain_Data.icc There are several implementations of the ICC in R . These can be used from Python via the rpy2 package. Example: from rpy2.robjects import DataFrame, FloatVector, IntVector

R - Warning message: “In cor(…): the standard deviation is zero”

陌路散爱 提交于 2019-12-03 22:24:52
I have a single vector of flow data (29 data) and a 3D matrix data(360*180*29) i want to find the correlation between single vector and 3D vector. The correlation matrix will have a size of 360*180. > str(ScottsCk_flow_1981_2010_JJA) num [1:29] 0.151 0.644 0.996 0.658 1.702 ... > str(ssta_winter) num [1:360, 1:180, 1:29] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... > summary(ssta_winter) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's -2.8 -0.2 0.1 0.2 0.6 6.0 596849.0 This above is the structure of the vector and 3D matrix. 3D matrix has many values as Null. > for (i in 1:360) { + for(j in 1:180){

Advanced MySQL: Find correlations between poll responses

倾然丶 夕夏残阳落幕 提交于 2019-12-03 21:05:35
I've got four MySQL tables: users (id, name) polls (id, text) options (id, poll_id, text) responses (id, poll_id, option_id, user_id) Given a particular poll and a particular option, I'd like to generate a table that shows which options from other polls are most strongly correlated. Suppose this is our data set: TABLE users: +------+-------+ | id | name | +------+-------+ | 1 | Abe | | 2 | Bob | | 3 | Che | | 4 | Den | +------+-------+ TABLE polls: +------+-----------------------+ | id | text | +------+-----------------------+ | 1 | Do you like apples? | | 2 | What is your gender? | | 3 | What

Function that converts a vector of numbers to a vector of standard units

谁说胖子不能爱 提交于 2019-12-03 19:13:03
问题 Is there a function that given a vector of numbers, returns another vector with the standard units corresponding to each value? where standard unit: how many SDs a value is + or - from the mean Example: x <- c(1,3,4,5,7) # note: mean = 4, sd = 2 foo(x) [1] -1.5 -0.5 0.0 0.5 1.5 Is this fictitious "foo" function already included in a package? 回答1: yes, scale() : x <- c(1,3,4,5,7) scale(x) 回答2: The function you are looking for is scale . scale(x) [,1] [1,] -1.3416408 [2,] -0.4472136 [3,] 0

Computing Autocorrelation with FFT Using JTransforms Library

人盡茶涼 提交于 2019-12-03 17:19:48
问题 I'm trying to calculate autocorrelation of sample windows in a time series using the code below. I'm applying FFT to that window, then computing magnitudes of real and imaginary parts and setting imaginary part to zero, lastly taking inverse transform of it to obtain autocorrelation: DoubleFFT_1D fft = new DoubleFFT_1D(magCnt); fft.realForward(magFFT); magFFT[0] = (magFFT[0] * magFFT[0]); for (int i = 1; i < (magCnt - (magCnt%2)) / 2; i++) { magFFT[2*i] = magFFT[2*i] * magFFT[2*i] + magFFT[2

Defining a function that calculates the covariance-matrix of a correlation-matrix

允我心安 提交于 2019-12-03 16:38:48
I have some problems with the transformation of a matrix and the names of the rows and columns. My problem is as follows: As input-matrix I have a (symmetric) correlation matrix like this one: The correlation-vector is given by the values of the lower triangular matrix: Now, I want to compute the variance-covariance-matrix of the these correlations, which are approximately normally distributed with the variance-covariance-matrix : The variances can be approximated by -> N is the sample size (in this example N = 66) The covariances can be approximated by For example the covariance between r_02