Philippe J. S. De Brouwer - The Big R-Book

Здесь есть возможность читать онлайн «Philippe J. S. De Brouwer - The Big R-Book» — ознакомительный отрывок электронной книги совершенно бесплатно, а после прочтения отрывка купить полную версию. В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: unrecognised, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

The Big R-Book: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «The Big R-Book»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

Introduces professionals and scientists to statistics and machine learning using the programming language R Written by and for practitioners, this book provides an overall introduction to R, focusing on tools and methods commonly used in data science, and placing emphasis on practice and business use. It covers a wide range of topics in a single volume, including big data, databases, statistical machine learning, data wrangling, data visualization, and the reporting of results. The topics covered are all important for someone with a science/math background that is looking to quickly learn several practical technologies to enter or transition to the growing field of data science. 
The Big R-Book for Professionals: From Data Science to Learning Machines and Reporting with R Provides a practical guide for non-experts with a focus on business users Contains a unique combination of topics including an introduction to R, machine learning, mathematical models, data wrangling, and reporting Uses a practical tone and integrates multiple topics in a coherent framework Demystifies the hype around machine learning and AI by enabling readers to understand the provided models and program them in R Shows readers how to visualize results in static and interactive reports Supplementary materials includes PDF slides based on the book’s content, as well as all the extracted R-code and is available to everyone on a Wiley Book Companion Site
is an excellent guide for science technology, engineering, or mathematics students who wish to make a successful transition from the academic world to the professional. It will also appeal to all young data scientists, quantitative analysts, and analytics professionals, as well as those who make mathematical models.

The Big R-Book — читать онлайн ознакомительный отрывок

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «The Big R-Book», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

картинка 78Question #8

Why does the second approach fail? Would you expect it to work?

It appears that while the object my_accexist, it is not possible to insert it in the definition of a new object – even while this inherits from the first. This makes sense, because the object “account” is not an attribute of the object “current account,” but its attributes become directly attributes of current account.

The object my_curr_accis now ready to be used. For example, we can change the balance.

my_curr_acc @balance <-500

Now, we will create an investment account. At this point, it becomes crucial to see that the object “custodian bank” is not a parent class, but rather an attribute. This means that before we can create an investment account, we need to define at least one custodian.

my_inv_acc <- new(“InvAcc”, custodian = my_cust_bank, holder=“Philippe”, branch=“DUB01”, opening_date = as.Date(“2019-02-21”)) # note that the first slot is another S4 object:my_inv_acc ## An object of class “InvAcc” ## Slot “custodian”: ## An object of class “Bnk” ## Slot “name”: ## [1] “HSBC” ## ## Slot “phone”: ## [1] “123123123” ## ## ## Slot “holder”: ## [1] “Philippe” ## ## Slot “branch”: ## [1] “DUB01” ## ## Slot “opening_date”: ## [1] “2019-02-21”

картинка 79Question #9

If you look careful at the code fragment above this question, you will notice that it is possible to provide an object my_cust_bankas attribute to the object my_inv_acc. This situation is similar to the code just above previous question, but unlike in the creation of also_an_account, now it works. Why is this?

To understand what happened here, we need to dig a little deeper.

my_inv_acc @custodian # our custodian bank is HSBC## An object of class “Bnk” ## Slot “name”: ## [1] “HSBC” ## ## Slot “phone”: ## [1] “123123123” my_inv_acc @custodian @name # note the cascade of @ signs## [1] “HSBC” my_inv_acc @custodian @name <-“DB” # change it to DBmy_inv_acc @custodian @name # yes, it is changed## [1] “DB” my_cust_bank @name # but our original bank isn't## [1] “HSBC” my_cust_bank @name <-“HSBC Custody” # try something differentmy_inv_acc @custodian @name # did not affect the account## [1] “DB” my_inv_acc @custodian @name <-my_cust_bank @name # change back

картинка 80Hint – List all slots

The function getSlots()will return a description of all the slots of a class:

getSlots(“Acc”) ## holder branch opening_date ## “character” “character” “Date” getSlots()

6.3.3 Validation of Input

While S3 provides no mechanism to check if all attributes are of the right type, creating an S4 object with the function new()will check if the slots are of the correct type. For example, if we try to create an account while providing a string for the balance (while a number is expected), then R will not create the new object and inform us of the mistake.

# Note the mistake in the following code:my_curr_acc <- new(“CurrAcc”, holder = “Philippe”, interest_rate = 0.01, balance=“0”, # Here is the mistake!branch = “LDN12”, opening_date= as.Date(“2018-12-01”)) ## Error in validObject(.Object): invalid class “CurrAcc” object: invalid object for slot “balance” in class “CurrAcc”: got class “character”, should be or extend class “numeric”

If you omit a slot, R coerces that slot to the default value.

x_account <- new(“CurrAcc”, holder = “Philippe”, interest_rate = 0.01, #no balance providedbranch = “LDN12”, opening_date= as.Date(“2018-12-01”)) x_account @balance # show what R did with it## numeric(0)

картинка 81Warning – Silent setting to default

Did you notice that R is silent about the missing balance? This is something to be careful with. If you forget that a default value has been assigned then this might lead to confusing mistakes.

An empty value for balance is not very useful and it can even lead to errors. Therefore, it is possible to assign default values with the function prototypewhen creating the class definition.

prototype()

setClass(“CurrAcc”, representation(interest_rate = “numeric”, balance = “numeric”), contains = “Acc”, prototype(holder = NA_character_, interst_rate = NA_real_, balance = 0)) x_account <- new(“CurrAcc”, # no holder # no interest rate # no balancebranch = “LDN12”, opening_date= as.Date(“2018-12-01”)) x_account # show what R did:## An object of class “CurrAcc” ## Slot “interest_rate”: ## numeric(0) ## ## Slot “balance”: ## [1] 0 ## ## Slot “holder”: ## [1] NA ## ## Slot “branch”: ## [1] “LDN12” ## ## Slot “opening_date”: ## [1] “2018-12-01”

картинка 82Warning – Changing class definitions at runtime

Most programming languages implement an OO system where class definitions are created when the code is compiled and instances of classes are created at runtime. During runtime, it is not possible to change the class definitions.

However, R is an interpreted language that is interactive and functional. The consequence is that it is possible to change class definitions at runtime (“while working in the R-terminal”). So it is possible to call setClass()again with the same object name, and R will assume that you want to change the previously defined class definition and silently override it. This can lead, for example, to the situation where different objects pretend to be of the same class, while they are not.

картинка 83Hint – Locking a class definition

To make sure that a previous class definition cannot be changed add sealed = TRUEto the call to setClass()

картинка 84Hint – Typesetting conventions

It is common practice to use “UpperCamelCase” for S4 class names. a Using a convention is always a good idea, using one that many other people use is even better. This convention avoids any confusion with the dispatcher functions that use the dot as separator.

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «The Big R-Book»

Представляем Вашему вниманию похожие книги на «The Big R-Book» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «The Big R-Book»

Обсуждение, отзывы о книге «The Big R-Book» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x