楼主: ipple7
5566 42

R Data Analysis Cookbook and R Data Visualization Cookbook [推广有奖]

21
Nicolle(未真实交易用户) 学生认证  发表于 2015-9-8 06:14:13

Classifying using the Naïve Bayes

提示: 作者被禁止或删除 内容自动屏蔽

22
Nicolle(未真实交易用户) 学生认证  发表于 2015-9-8 06:17:06
提示: 作者被禁止或删除 内容自动屏蔽

23
Nicolle(未真实交易用户) 学生认证  发表于 2015-9-8 06:17:20
提示: 作者被禁止或删除 内容自动屏蔽

24
Nicolle(未真实交易用户) 学生认证  发表于 2015-9-8 06:28:25
提示: 作者被禁止或删除 内容自动屏蔽

25
Nicolle(未真实交易用户) 学生认证  发表于 2015-9-8 06:29:34

Classifying using linear discriminant function analysis

提示: 作者被禁止或删除 内容自动屏蔽

26
Nicolle(未真实交易用户) 学生认证  发表于 2015-9-8 06:30:48
提示: 作者被禁止或删除 内容自动屏蔽

27
Nicolle(未真实交易用户) 学生认证  发表于 2015-9-8 06:33:28
提示: 作者被禁止或删除 内容自动屏蔽

28
Lisrelchen(未真实交易用户) 发表于 2015-9-10 08:28:41

Creating and examining date objects

  1. How to do it...
  2. Internally, R represents dates as the number of days from 1 January, 1970:

  3. Get today's date:
  4. > Sys.Date()
  5. Create a date object from a string:
  6. > # Supply year as two digits
  7. > # Note correspondence between separators in the date string and the format string
  8. > as.Date("1/1/80", format = "%m/%d/%y")

  9. [1] "1980-01-01"

  10. > # Supply year as 4 digits
  11. > # Note uppercase Y below instead of lowercase y as above
  12. > as.Date("1/1/1980", format = "%m/%d/%Y")

  13. [1] "1980-01-01"

  14. > # If you omit format string, you must give date as "yyyy/mm/dd" or as "yyyy-mm-dd"
  15. > as.Date("1970/1/1")

  16. [1] "1970-01-01"

  17. > as.Date("70/1/1")

  18. [1] "0070-01-01"
  19. Use other options for separators (this example uses hyphens) in the format string, and also see the underlying numeric value:
  20. > dt <- as.Date("1-1-70", format = "%m-%d-%y")
  21. > as.numeric(dt)  

  22. [1] 0
  23. Explore other format string options:
  24. > as.Date("Jan 15, 2015", format = "%b %d, %Y")

  25. [1] "2015-01-15"

  26. > as.Date("January 15, 15", format = "%B %d, %y")

  27. [1] "2015-01-15"
  28. Create dates from numbers by typecasting:
  29. > dt <- 1000
  30. > class(dt) <- "Date"
  31. > dt                 # 1000 days from 1/1/70

  32. [1] "1972-09-27"

  33. > dt <- -1000
  34. > class(dt) <- "Date"
  35. > dt                 # 1000 days before 1/1/70

  36. [1] "1967-04-07"
  37. Create dates directly from numbers by setting the origin date:
  38. > as.Date(1000, origin = as.Date("1980-03-31"))

  39. [1] "1982-12-26"

  40. > as.Date(-1000, origin = as.Date("1980-03-31"))

  41. [1] "1977-07-05"
  42. Examine date components:
  43. > dt <- as.Date(1000, origin = as.Date("1980-03-31"))
  44. > dt

  45. [1] "1982-12-26"

  46. > # Get year as four digits
  47. > format(dt, "%Y")

  48. [1] "1982"

  49. > # Get the year as a number rather than as character string
  50. > as.numeric(format(dt, "%Y"))

  51. [1] 1982

  52. > # Get year as two digits
  53. > format(dt, "%y")

  54. [1] "82"

  55. > # Get month
  56. > format(dt, "%m")

  57. [1] "12"

  58. > as.numeric(format(dt, "%m"))

  59. [1] 12

  60. > # Get month as string
  61. > format(dt, "%b")

  62. [1] "Dec"

  63. > format(dt, "%B")

  64. [1] "December"

  65. > months(dt)

  66. [1] "December"

  67. > weekdays(dt)

  68. [1] "Sunday"

  69. > quarters(dt)
  70. [1] "Q4"

  71. > julian(dt)

  72. [1] 4742
  73. attr(,"origin")
  74. [1] "1970-01-01"

  75. > julian(dt, origin = as.Date("1980-03-31"))

  76. [1] 1000
  77. attr(,"origin")
  78. [1] "1980-03-31"
复制代码

29
Lisrelchen(未真实交易用户) 发表于 2015-9-10 08:31:06
  1. Operating on date objects

  2. R supports many useful manipulations with date objects such as date addition and subtraction, and the creation of date sequences. This recipe shows many of these operations in action. For details on creating and examining date objects, see the previous recipe Creating and examining date objects, in this chapter.

  3. Getting ready
  4. The base R package provides date functionality, and you do not need any preparatory steps.

  5. How to do it...
  6. Perform the addition and subtraction of days from date objects:
  7. > dt <- as.Date("1/1/2001", format = "%m/%d/%Y")
  8. > dt

  9. [1] "2001-01-01"

  10. > dt + 100                 # Date 100 days from dt

  11. [1] "2001-04-11"

  12. > dt + 31

  13. [1] "2001-02-01"
  14. Subtract date objects to find the number of days between two dates:
  15. > dt1 <- as.Date("1/1/2001", format = "%m/%d/%Y")
  16. > dt2 <- as.Date("2/1/2001", format = "%m/%d/%Y")
  17. > dt1-dt1

  18. Time difference of 0 days

  19. > dt2-dt1

  20. Time difference of 31 days

  21. > dt1-dt2

  22. Time difference of -31 days

  23. > as.numeric(dt2-dt1)

  24. [1] 31
  25. Compare date objects:
  26. > dt2 > dt1

  27. [1] TRUE

  28. > dt2 == dt1

  29. [1] FALSE
  30. Create date sequences:
  31. > d1 <- as.Date("1980/1/1")
  32. > d2 <- as.Date("1982/1/1")
  33. > # Specify start date, end date and interval
  34. > seq(d1, d2, "month")

  35. [1] "1980-01-01" "1980-02-01" "1980-03-01" "1980-04-01"
  36. [5] "1980-05-01" "1980-06-01" "1980-07-01" "1980-08-01"
  37. [9] "1980-09-01" "1980-10-01" "1980-11-01" "1980-12-01"
  38. [13] "1981-01-01" "1981-02-01" "1981-03-01" "1981-04-01"
  39. [17] "1981-05-01" "1981-06-01" "1981-07-01" "1981-08-01"
  40. [21] "1981-09-01" "1981-10-01" "1981-11-01" "1981-12-01"
  41. [25] "1982-01-01"

  42. > d3 <- as.Date("1980/1/5")
  43. > seq(d1, d3, "day")

  44. [1] "1980-01-01" "1980-01-02" "1980-01-03" "1980-01-04"
  45. [5] "1980-01-05"

  46. > # more interval options
  47. > seq(d1, d2, "2 months")

  48. [1] "1980-01-01" "1980-03-01" "1980-05-01" "1980-07-01"
  49. [5] "1980-09-01" "1980-11-01" "1981-01-01" "1981-03-01"
  50. [9] "1981-05-01" "1981-07-01" "1981-09-01" "1981-11-01"
  51. [13] "1982-01-01"

  52. > # Specify start date, interval and sequence length
  53. > seq(from = d1, by = "4 months", length.out = 4 )

  54. [1] "1980-01-01" "1980-05-01" "1980-09-01" "1981-01-01"
  55. Find a future or past date from a given date, based on an interval:
  56. > seq(from = d1, by = "3 weeks", length.out = 2)[2]

  57. [1] "1980-01-22"
复制代码

30
Lisrelchen(未真实交易用户) 发表于 2015-9-10 08:33:35
  1. Performing preliminary analyses on time series data

  2. Before creating proper time series objects, we may want to do some preliminary analyses. This recipe shows you how.

  3. Getting ready
  4. The base R package provides all the necessary functionality. If you have not already downloaded the data files for this chapter, please do it now and ensure that they are located in your R working directory.

  5. How to do it...
  6. Read the file. We will use a data file that has the share prices of Walmart (downloaded from Yahoo Finance) between March 11, 1999 and January 15, 2015:
  7. > wm <- read.csv("walmart.csv")
  8. View the data as a line chart:
  9. > plot(wm$Adj.Close, type = "l")

  10. How to do it...
  11. Compute and plot daily price movements:
  12. > d <- diff(wm$Adj.Close)
  13. > plot(d, type = "l")
  14. The plotted daily price movements appear as follows:

  15. How to do it...
  16. Generate a histogram of the daily price changes, along with a density plot:
  17. > hist(d, prob = TRUE, ylim = c(0,0.8), main = "Walmart stock", col = "blue")
  18. > lines(density(d), lwd = 3)

  19. How to do it...
  20. Compute one-period returns:
  21. > wmm <- read.csv("walmart-monthly.csv")
  22. > wmm.ts <- ts(wmm$Adj.Close)
  23. > d <- diff(wmm.ts)
  24. > wmm.return <- d/lag(wmm.ts, k=-1)
  25. > hist(wmm.return, prob = TRUE, col = "blue")
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-25 03:12