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», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

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

Интервал:

Закладка:

Сделать

6.3.1 Creating S4 Objects

While an S3 object can be used without defining it first, to create a valid S4 object, we need at least:

Name: An alpha-numeric string that identifies the class

Representation: A list of slots (or attributes), giving their names and classes. For example, a person class might be represented by a character name and a numeric age, as follows: representation(name = “character”, age = “numeric”)

Inheritance: A character vector of classes that it inherits from, or in S4 terminology, contains. Note that S4 supports multiple inheritance, but this should be used with extreme caution as it makes method lookup extremely complicated.

S4 objects are created with the function setClass().

setClass()

# Create the object type Acc to hold bank-accounts: setClass(“Acc”, representation(holder = “character”, branch = “character”, opening_date = “Date”)) # Create the object type Bnk (bank): setClass(“Bnk”, representation(name = “character”, phone = “numeric”)) # Define current account as a child of Acc: setClass(“CurrAcc”, representation(interest_rate = “numeric”, balance = “numeric”), contains = “Acc”) # Define investment account as a child of Acc setClass(“InvAcc”, representation(custodian = “Bnk”), contains = “Acc”)

This will only create the definition of the objects. So, to create a variable in your code that can be used to put your data or models inside, instances have to be created with the function new().

new()

картинка 74Note – Difference between inheritance and methods

Note the difference in syntax – for the function setClass– between how the argument representationand the argument containstake values. The representationcand we can create a firstode> argument takes a function and hence, more arguments can be passed by adding them comma separated. In order to passmore than one parent class to contains, one needs to provide a character vector (for example c(“InvAcc”,“Acc”)).

Both the arguments slotsand containswill readily use S4 classes and the implicit class of a base type. In order to use S3 classes, one needs first to register them with setOldClass(). If we do not want type control when an instance of a class is generated, we can provide to the slotsargument a special class “ANY” (this tell R not to restrict the input).

You might not have noticed right away, but we started off with a complex problem where some objects depend on others (in OO we speak about “parents” and “children”) and even where some objects take others as attributes. Those two things are very different and a little tricky to understand.

At this point, the classes Bnkand Accexist and we can create a first instance for both.

# Create an instance of Bnk:my_cust_bank <- new(“Bnk”, name = “HSBC”, phone = 123456789) # Create an instance of Acc:my_acc <- new(“Acc”, holder = “Philippe”, branch = “BXL12”, opening_date = as.Date(“2018-10-02”))

6.3.2 Using S4 Objects

Now, we have two S4 objects and we can use them in our code as necessary. For example, we can change the phone number.

# Check if it is really an S4 object: isS4(my_cust_bank) ## [1] TRUE # Change the phone number and check:my_cust_bank @phone =987654321 # change the phone number print(my_cust_bank @phone) # check if it changed## [1] 987654321

картинка 75Note – Compare addressing slots in S4 and S3

In order to access slots of an S4 object, we use @, not $:

There is also a specific function to get attributes froman object: attr(). This function allows to create attributes, change them or even remove them (by setting them to NULL)

attr()

# This will do the same as my_cust_bank@phone: attr(my_cust_bank, ‘phone’) ## [1] 987654321 # The function also allows partial matching: attr(my_cust_bank, which=’ph’, exact = FALSE) ## [1] 987654321 # attr can also change the value of an attribute. attr(my_cust_bank, which=’phone’) <-‘123123123’ # Let us verify:my_cust_bank @phone ## [1] “123123123” # It is even possible to create a new attribute or remove one. attr(my_cust_bank, ‘something’) <-‘Philippe’ attr(my_cust_bank, ‘something’) ## [1] “Philippe” attr(my_cust_bank, ‘something’) <- NULL attr(my_cust_bank, ‘something’) ## NULL str(my_cust_bank) # the something attribute is totally gone## Formal class ‘Bnk’ [package “.GlobalEnv”] with 2 slots ## ..@ name : chr “HSBC” ## ..@ phone: chr “123123123”

картинка 76Warning – Partialmatching

While the function attr()allows partial matching. It is never a good idea to use partial matching in a batch environment. This can lead to hard to detect programming errors.

Some slots – like class, comment, dim, dimnames, names, row.namesand tsp(for time series objects) – are special: they can only take some values. This knowledge can even be used to change those attributes.

x <-1 :9 x # x is a vector## [1] 1 2 3 4 5 6 7 8 9 class(x) ## [1] “integer” attr(x, “dim”) <- c(3,3) x # is is now a matrix!## [,1] [,2] [,3] ## [1,] 1 4 7 ## [2,] 2 5 8 ## [3,] 3 6 9 class(x) # but R is not fooled.## [1] “matrix”

картинка 77Hint – Alternative to address slots

Alternatives to access slots (attributes) include the function slot(), that works like [[for regular objects.

slot(my_acc, “holder”) ## [1] “Philippe”

slot()

The object my_accis actually not very useful. It is a structure that would be in common for all types of accounts (e.g. investment accounts, savings accounts and current accounts). However, no bank would just sell and empty structure account. So, let us open a current account first.

my_curr_acc <- new(“CurrAcc”, holder = “Philippe”, interest_rate = 0.01, balance=, branch = “LDN12”, opening_date= as.Date(“2018-12-01”)) # Note that the following does not work and is bound to fail:also_an_account <- new(“CurrAcc”, holder = “Philippe”, interest_rate = 0.01, balance=, Acc=my_acc) ## Error in initialize(value, …): invalid name for slot of class “CurrAcc”: Acc

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

Интервал:

Закладка:

Сделать

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

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


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

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

x