簡體   English   中英

Spearman 相關和拆分 1 個變量

[英]Spearman correlation and splitting 1 variable

       Year.Sales.Advertise.Employees
1               1985 1.05 162 32 
2               1986 1.26 285 47 
3               1987 1.47 540 23 
4               1988 2.16 261 68 
5               1989 1.95 360 32 
6                1990 2.4 690 17 
7               1991 2.37 495 58 
8               1992 3.15 948 75 
9               1993 3.57 720 98 
10             1994 4.41 1.14 43 
11             1995 4.5 1.395 76 
12             1996 5.61 1.56 89 
13            1997 5.19 1.38 108 
14             1998 5.67 1.26 76 
15             1999 5.16 1.71 65 
16              2000 6.84 1.86 93

我想找到銷售和廣告之間的斯皮爾曼相關性,我被困了 3 個小時,請幫忙。 我想我必須將 1 個變量分成 5 個變量,但我很掙扎。

我們可以使用strsplit來分割我們的數據,即

new_df <- setNames(data.frame(do.call(rbind, strsplit(df2$Year.Sales.Advertise.Employees, ' '))), 
                   strsplit(names(df2), '.', fixed = TRUE)[[1]])

這使,

 Year Sales Advertise Employees 1 1985 1.05 162 32 2 1986 1.26 285 47 3 1987 1.47 540 23 4 1988 2.16 261 68 5 1989 1.95 360 32 6 1990 2.4 690 17 7 1991 2.37 495 58 8 1992 3.15 948 75 9 1993 3.57 720 98 10 1994 4.41 1.14 43 11 1995 4.5 1.395 76 12 1996 5.61 1.56 89 13 1997 5.19 1.38 108 14 1998 5.67 1.26 76 15 1999 5.16 1.71 65 16 2000 6.84 1.86 93

然后,您可以使用cor (即cor(new_df$Advertise, new_df$Employees) )來查找您想要的任何列之間的相關性。

NOTE1:確保您的初始列是一個字符(不是因子)

NOTE2:默認情況下, cor函數計算皮爾遜相關性。 對於 spearman,添加參數cor(..., method = "spearman") ,如@Base_R_Best_R 所述。

數據

dput(df2)
structure(list(Year.Sales.Advertise.Employees = c("1985 1.05 162 32", 
"1986 1.26 285 47", "1987 1.47 540 23", "1988 2.16 261 68", "1989 1.95 360 32", 
"1990 2.4 690 17", "1991 2.37 495 58", "1992 3.15 948 75", "1993 3.57 720 98", 
"1994 4.41 1.14 43", "1995 4.5 1.395 76", "1996 5.61 1.56 89", 
"1997 5.19 1.38 108", "1998 5.67 1.26 76", "1999 5.16 1.71 65", 
"2000 6.84 1.86 93")), class = "data.frame", row.names = c(NA, 
-16L))

不確定您是否正在尋找以下內容或其他內容

# split strings into separate columns
df <- `names<-`(data.frame(t(apply(df, 1, function(x) as.numeric(unlist(strsplit(x,split = " ")))))),
          unlist(strsplit(names(df),split = "\\.")))

# calculate correction coefficient
r <- cor(df$Sales,df$Advertise)

以至於

> r
[1] -0.5624524

數據

df <- structure(list(Year.Sales.Advertise.Employees = c("1985 1.05 162 32", 
"1986 1.26 285 47", "1987 1.47 540 23", "1988 2.16 261 68", "1989 1.95 360 32", 
"1990 2.4 690 17", "1991 2.37 495 58", "1992 3.15 948 75", "1993 3.57 720 98", 
"1994 4.41 1.14 43", "1995 4.5 1.395 76", "1996 5.61 1.56 89", 
"1997 5.19 1.38 108", "1998 5.67 1.26 76", "1999 5.16 1.71 65", 
"2000 6.84 1.86 93")), class = "data.frame", row.names = c(NA, 
-16L))

> df
   Year.Sales.Advertise.Employees
1                1985 1.05 162 32
2                1986 1.26 285 47
3                1987 1.47 540 23
4                1988 2.16 261 68
5                1989 1.95 360 32
6                 1990 2.4 690 17
7                1991 2.37 495 58
8                1992 3.15 948 75
9                1993 3.57 720 98
10              1994 4.41 1.14 43
11              1995 4.5 1.395 76
12              1996 5.61 1.56 89
13             1997 5.19 1.38 108
14              1998 5.67 1.26 76
15              1999 5.16 1.71 65
16              2000 6.84 1.86 93

如果您要求將數據拆分為 4 個離散列,則應該這樣做。

您在問題中的數據需要一些清理。 它可能需要更多(手動)清潔,因為在 1993 年和 1994 年之間,廣告從 720 下降到 1.14。這可能是從數十萬到數百萬。

x <- c("1985 1.05 162 32",
  "1986 1.26 285 47",
  "1987 1.47 540 23",
  "1988 2.16 261 68",
  "1989 1.95 360 32",
  "1990 2.4 690 17",
  "1991 2.37 495 58",
  "1992 3.15 948 75",
  "1993 3.57 720 98",
  "1994 4.41 1.14 43",
  "1995 4.5 1.395 76",
  "1996 5.61 1.56 89",
  "1997 5.19 1.38 108",
  "1998 5.67 1.26 76",
  "1999 5.16 1.71 65",
  "2000 6.84 1.86 93")

library(tidyverse)
clean_df <- x %>% 
  as.data.frame() %>% 
  separate('.',
           into = c('year','sales', 'advertise', 'empl'), 
           sep = ' ') %>%
  as_tibble() %>%
  mutate_all(as.numeric)

cor(clean_df$sales, clean_df$advertise, method = 'spearman')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM